I am using ajax to call a php script (wordpress). I get status code 200 OK but I dont get any data back.. My script dies, when trying to create an instant of an object. But I cant get any info why..
// create Order object
echo "this gets returned";
try {
if(new Order($auth_login, $auth_password, $caleo_live_mode)) {
echo "works";
} else {
echo "if error";
}
} catch (Exception $e) {
echo "try fail";
}
echo "This does not";
Nothing inside the try block is getting returned.
What can I do to try to see what is going on?
Moved solution from question to answer:
ANSWER:
I figured it out....
I read through the source code of Order and found a try/catch statement which looked like:
catch (Exception $e) {
exit();
}
Related
In the following over simplified scenario, I'm wondering if firstFunction() fails, will secondFunction() still run but then show the error, or will it break out of the try block when firstFunction fails?
try {
firstFunction();
secondfunction();
} catch (\Exception $e) {
echo $e...
}
I am using RabbitMQ with PHP. While consuming messages from RabbitMQ, we have magic in this piece of code:
while (count($callbacks)) {
try {
$conn->wait();
} catch (Exception $e) {
//Log the message
}
}
This is working as infinite loop to receive messages as expected but what happening is if we lost connection to the RabbitMQ/RabbitMQ not up its going to catch block and returns nothing and printing bulk log messages. Is there any better way we can check for connection for RabbitMQ and stops the script? How can we achieve this? Any suggestions?
while (count($callbacks)) {
try {
$conn->wait();
} catch (Exception $e) {
//Log the message
break;
}
}
I'm using the Respect\Validation library and I cannot get meaningful exceptions to print out apart from the first message.
This code gives me an error:
try {
$foo = \Respect\Validation\Validator::notEmpty();
$foo->assert('');
} catch (\InvalidArgumentException $e) {
echo $e->getFullMessage();
}
Error code: ERR_CONNECTION_RESET
However, this code works fine:
try {
$foo = \Respect\Validation\Validator::notEmpty();
$foo->check('');
} catch (\InvalidArgumentException $e) {
echo $e->getMainMessage();
}
I've tried turning off Xdebug but that didn't solve the problem. My version of PHP is 5.4.3.
Consider these two examples
<?php
function throw_exception() {
// Arbitrary code here
throw new Exception('Hello, Joe!');
}
function some_code() {
// Arbitrary code here
}
try {
throw_exception();
} catch (Exception $e) {
echo $e->getMessage();
}
some_code();
// More arbitrary code
?>
and
<?php
function throw_exception() {
// Arbitrary code here
throw new Exception('Hello, Joe!');
}
function some_code() {
// Arbitrary code here
}
try {
throw_exception();
} catch (Exception $e) {
echo $e->getMessage();
} finally {
some_code();
}
// More arbitrary code
?>
What's the difference? Is there a situation where the first example wouldn't execute some_code(), but the second would? Am I missing the point entirely?
If you catch Exception (any exception) the two code samples are equivalent. But if you only handle some specific exception type in your class block and another kind of exception occurs, then some_code(); will only be executed if you have a finally block.
try {
throw_exception();
} catch (ExceptionTypeA $e) {
echo $e->getMessage();
}
some_code(); // Will not execute if throw_exception throws an ExceptionTypeB
but:
try {
throw_exception();
} catch (ExceptionTypeA $e) {
echo $e->getMessage();
} finally {
some_code(); // Will be execute even if throw_exception throws an ExceptionTypeB
}
fianlly block is used when you want a piece of code to execute regardless of whether an exception occurred or not...
Check out Example 2 on this page :
PHP manual
Finally will trigger even if no exception were caught.
Try this code to see why:
<?php
class Exep1 extends Exception {}
class Exep2 extends Exception {}
try {
echo 'try ';
throw new Exep1();
} catch ( Exep2 $e)
{
echo ' catch ';
} finally {
echo ' finally ';
}
echo 'aftermath';
?>
the output will be
try finally
Fatal error: Uncaught exception 'Exep1' in /tmp/execpad-70360fffa35e/source-70360fffa35e:7
Stack trace:
#0 {main}
thrown in /tmp/execpad-70360fffa35e/source-70360fffa35e on line 7
here is fiddle for you. https://eval.in/933947
From the PHP manual:
In PHP 5.5 and later, a finally block may also be specified after or instead of catch blocks. Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.
See this example in the manual, to see how it works.
http://www.youtube.com/watch?v=EWj60p8esD0
Watch from: 12:30 onwards
Watch this video.
The language is JAVA though.
But i think it illustrates Exceptions and the use of finally keyword very well.
I was challenged how to break or end execution of a parent function without modifying the code of the parent, using PHP
I cannot figure out any solution, other than die(); in the child, which would end all execution, and so anything after the parent function call would end. Any ideas?
code example:
function victim() {
echo "I should be run";
killer();
echo "I should not";
}
function killer() {
//code to break parent here
}
victim();
echo "This should still run";
function victim() {
echo "I should be run";
killer();
echo "I should not";
}
function killer() {
throw new Exception('Die!');
}
try {
victim();
} catch (Exception $e) {
// note that catch blocks shouldn't be empty :)
}
echo "This should still run";
Note that Exceptions are not going to work in the following scenario:
function victim() {
echo "this runs";
try {
killer();
}
catch(Exception $sudden_death) {
echo "still alive";
}
echo "and this runs just fine, too";
}
function killer() { throw new Exception("This is not going to work!"); }
victim();
You would need something else, the only thing more robust would be something like installing your own error handler, ensure all errors are reported to the error handler and ensure errors are not converted to exceptions; then trigger an error and have your error handler kill the script when done. This way you can execute code outside of the context of killer()/victim() and prevent victim() from completing normally (only if you do kill the script as part of your error handler).