as the title says, try/catch doesn't catch throwed exception.
Here is my code:
public function edit($id)
{
try {
$this->permissions($id);
return $this->redirect(['edit/list']);
} catch (Exception $e) {
Yii::$app->session->setFlash('error', Yii::t('app', 'PERMISSIONS_NOT_FOUND'));
}
}
I'm trying to catch an exception which is in permissions() method. Permissions method is:
private function persimssions($id)
{
$permitted = false;
if (!$permitted) {
throw new ForbiddenHttpException(Yii::t('app', 'MODULE_NO_PERMISSIONS'));
}
}
Could someone explain me what is wrong? Thanks for any help
I almost sure that your Exception is in fact yii\db\Exception - check this in use statements. If so it's obvious that it will not catch ForbiddenHttpException.
If you want to catch all exceptions write \Exception in catch or add proper use statement.
Related
I am new to PHP and trying to active below:
I am catching multiple custom exceptions including standard exception.
I want throw these exceptions as another custom exception and based on the original exception I want to print a message.
example.php
try {
// Do something
} catch (CustomeException1 $ex) {
} catch (CustomException2 $ex) {
} catch (Exception $ex) {
}
I want to write something like FinalCustomException.php
class FinalCustomException extends Exception {
public static fromException(CustomException1 $ex) {
//print something
}
public static fromException(CustomException2 $ex) {
//print something
}
public static gotException() {
//call above fromException method based on the exception occurred
}
}
I am not sure How to do this. Can anyone please help me with this?
Thanks in advance.
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 am learning how to use Exceptions in PHP. In a subfunction of my code, I want to throw an Exception to stop the main function if an error appears.
I have three functions:
function main_buildt_html(){
...
check_if_parameters_are_ok();
// if the subfunction_check exception is thrown, don't execute the process below and go to an error page
...
}
function check_if_parameters_are_ok(){
...
try{
...
subfunction_check();
...
}catch(Exception $e){
}
...
}
function subfunction_check(){
...
if ($some_error) throw new Exception("Its not ok ! stop the process and redirect the user to an error page");
...
}
From my main "main_buildt_html" function, how can I properly detect if an exception has been thrown?
I want to detect the "subfunction" exception from the main function so I can stop the standard process and redirect the user to an error HTML page.
Normally the exception will be throwin up until the highest level in the chain, or when you catch it in any level.
in your case, if you want to catch the exception in check_if_parameters_are_ok() and main_buildt_html() functions, you need to throw the exception up in the check_if_parameters_are_ok() function.
function check_if_parameters_are_ok(){
...
try{
...
subfunction_check();
...
}catch(Exception $e){
//handle exception.
throw $e; // throw the excption again
}
}
now you need to catch the excption in the main_buildt_html() function.
function main_buildt_html(){
try {
check_if_parameters_are_ok();
} catch (Exception $e) {
// handle the excption
}
}
check_if_parameters_are_ok() should return false when it catches the error. The main function should test the value.
function main_buildt_html(){
...
if (check_if_parameters_are_ok()) {
...
} else {
...
}
}
function check_if_parameters_are_ok(){
...
try{
...
subfunction_check();
...
}catch(Exception $e){
return false;
}
...
}
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'm in doubt about exception handling.
If I have a function foo(); is it same as if I handle it like this:
try {
foo();
} catch (Exception $e) {
// do someting
}
or if I do it in function like this:
foo() {
try {
// function body
} catch (Exception $e) {
// do someting
}
}
And what if I do not throw Exception ? Code will continue to execute even if error appears ?
Yes, code will continue. In php exists errors and exceptions. You can handle errors with function set_error_handler(), handle uncathable exceptions with function set_exception_handler() and you can handle exceptions using try .. catch