I've setup the ZendSkeletonApplication with ZF 2.0.3 and I am unable to catch exceptions in my controllers. For instance if I put the below piece of code in module/Application/src/Application/Controller/IndexController.php:
public function indexAction() {
echo "BEFORE\n";
try {
throw new \Exception("My exception");
} catch (Exception $e) {
echo "Caught exception $e\n";
exit;
}
and access the page I get:
BEFORE
An error occurred
An error occurred during execution; please try again later.
Additional information:
Exception
File:
module/Application/src/Application/Controller/IndexController.php:25
Message:
My exception
the ViewModel kicks in and displays the exception, effectively preventing me from catching it.
How can I catch exceptions in ZF2 controllers?
You are throwing PHP's generic Exception
throw new \Exception("My exception");
but you catch the Exception from the current namespace
} catch (Exception $e) {
Assuming your controller is in Application\Controller, you either have to declare
use \Exception;
above your class to import the global Exception into the current namespace or
} catch (\Exception $e) {
to catch PHP's global Exception.
Related
Exception
Exception 'Error' with message 'Class 'app\commands\CallLogs' not
found'
is not able to get caught in catch block.
Code:
I tried with calling undefined class just to see how and what exception catch block catches.
public function actionTest(){
try {
$logs = new CallLogs();
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
But, When I intentionally throw any exception, it works.
public function actionTest(){
try {
throw new \yii\base\Exception('hello');
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
I have tried with base\Exception class and \ErrorException class. But, no help.
Any help/hint is appreciable
catch (\Throwable $e) will do the job
\Throwable was introduced back in PHP 7.0 and is (quoting from docs) used for
[...] any object that can be thrown via a throw statement, including
Error and Exception.
I'm trying to do a very basic exception try catch, but it doesn't catch.
$id =0;
try {
$question = $this->model->find($id); // will not find anything since $id = 0
$question->delete(); // throw an exception
return true;
} catch (\Exception $e) {
dd ('hello'); // should end up here, but no?!?!?
} catch (FatalThrowableError $f) {
echo ("fatal"); // or here... but no.
}
but the catch doesn't "catch". I get an Fatal error in the browser saying that delete was called on a null object. But that's exactly what I was trying to do: do a delete on a null object (id = 0 is not in the DB), to test the exception.
I have tried
use Symfony\Component\Debug\Exception;
use Symfony\Component\Debug\Exception\FatalThrowableError;
or simply
Exception;
FatalThrowableError;
Also, having the \Exception $e or Exception $e (with or without ) doesn't change anything.
Note that if I add a line like $foo = 4/0 I get into the Exception section (dd (hello)).
in .env APP_DEBUG=true, APP_LOG_LEVEL=debug
I'm on Laravel 5.5 using PHP 7.0.10 on windows 7.
http://php.net/manual/en/language.errors.php7.php
As the Error hierarchy does not inherit from Exception, code that uses
catch (Exception $e) { ... } blocks to handle uncaught exceptions in
PHP 5 will find that these Errors are not caught by these blocks.
Either a catch (Error $e) { ... } block or a set_exception_handler()
handler is required.
You can, additionally, catch (\Throwable $e) {} to account for both Error and Exception types.
I don't understand why this code:
class MyException extends Exception {};
try {
try {
throw new MyException;
} catch (Exception $e) {
echo "1:";
throw $e;
} catch (MyException $e) {
echo "2:";
throw $e;
}
}
catch (Exception $e) {
echo get_class($e);
}
Returns: 1:MyException.
Isn't it supposed to catch the second one MyException and therefore return 2?
I thought with multiple exceptions it looks for the current try/catch first, but it looks like it catches the exception from the first try? or is it because MyException is empty and it uses Exception instead?
Exception here is a base class for your MyException class. Your $e variable has class MyException, so everything is right. If you make:
echo "1:";
var_dump($e);
throw $e;
you will see that $e is object(MyException). You haven't cast types, you just using polymorphism.
All your objects that have type Exception or it's subtypes will be caught in the 1-st block. Code will be executed in first by order block that can apply the exception.
Catch blocks are processed in the order they appear. Your code for catching MyException will never be called, because all subclasses of Exception are caught in your first catch block.
I want to implement a good error handling in my app, I have forced this file for catching the error.
App\Services\PayUService
try {
$this->buildXMLHeader; // Should be $this->buildXMLHeader();
} catch (Exception $e) {
return $e;
}
App\Controller\ProductController
function secTransaction(){
if ($e) {
return view('products.error', compact('e'));
}
}
And this is what I get.
I don't know why Laravel is not redirecting me to the view.
Is the error forced right?
You are inside a namespace so you should use \Exception to specify the global namespace:
try {
$this->buildXMLHeader();
} catch (\Exception $e) {
return $e->getMessage();
}
In your code you've used catch (Exception $e) so Exception is being searched in/as:
App\Services\PayUService\Exception
Since there is no Exception class inside App\Services\PayUService so it's not being triggered. Alternatively, you can use a use statement at the top of your class like use Exception; and then you can use catch (Exception $e).
I have a custom error handlers:
set_error_handler('API_Error_Handler');
register_shutdown_function('Fatal_Error_Handler'); // This one calls API_Error_Handler eventually
In the following example, both catch{} section AND API_Error_Handler are executed.
try{
// Exception raised here
} catch(Exception $e){
// No error reporting needed, do something else
}
I want ONLY catch{} to execute. How do I do that? Maybe determine within API_Error_Handler whether exception is already caught by try-catch? Or are there other approaches available?
Example code:
set_error_handler(function() {
echo "Error is handled by custom error handler. <br>";
});
try{
new SoapClient('http://bad.address/wsdl');
} catch(Exception $e){
echo "Error is caught. <br>";
}
I think the best way will be to create an Exception class by your self that extends Exception:
class MyCustomException extends \Exception {}
and throw this where you need it. Then change
} catch (Exception $e)
to
} catch (MyCustomException $e)
and you should only get your custom exception catched