PHP - try and catch block (Exception) handling - php

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

Related

Try/catch doesn't catch an exception in Yii2

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.

How to manage exceptions in multiple nested functions?

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;
}
...
}

PHP exception inside catch: how to handle it?

Suppose to have a PHP code inside a try...catch block. Suppose that inside catch you would like to do something (i.e. sending email) that could potentially fail and throw a new exception.
try {
// something bad happens
throw new Exception('Exception 1');
}
catch(Exception $e) {
// something bad happens also here
throw new Exception('Exception 2');
}
What is the correct (best) way to handle exceptions inside catch block?
Based on this answer, it seems to be perfectly valid to nest try/catch blocks, like this:
try {
// Dangerous operation
} catch (Exception $e) {
try {
// Send notification email of failure
} catch (Exception $e) {
// Ouch, email failed too
}
}
You should not throw anything in catch. If you do so, than you can omit this inner layer of try-catch and catch exception in outer layer of try-catch and process that exception there.
for example:
try {
function(){
try {
function(){
try {
function (){}
} catch {
throw new Exception("newInner");
}
}
} catch {
throw new Exception("new");
}
}
} catch (Exception $e) {
echo $e;
}
can be replaced to
try {
function(){
function(){
function (){
throw new Exception("newInner");
}
}
}
} catch (Exception $e) {
echo $e;
}
You have 2 possible ways:
You exit the program (if it is severe) and you write it to a log file and inform the user.
If the error is specifically from your current class/function,
you throw another error, inside the catch block.
You can use finally. Code in this branch will be executed even if exception is thrown within catch branch

Get all the exceptions from one try catch block

I wonder if it's posible to get all the exceptions throwed.
public function test()
{
$arrayExceptions = array();
try {
throw new Exception('Division by zero.');
throw new Exception('This will never get throwed');
}
catch (Exception $e)
{
$arrayExceptions[] = $e;
}
}
I have a huge try catch block but i want to know all the errors, not only the first throwed. Is this possible with maybe more than one try or something like that or i am doing it wrong?
Thank you
You wrote it yourself: "This will never get throwed" [sic].
Because the exception will never get thrown, you cannot catch it. There only is one exception because after one exception is thrown, the whole block is abandoned and no further code in it is executed. Hence no second exception.
Maybe this was what the OP was actually asking for. If the function is not atomic and allows for some level of fault tolerance, then you can know all the errors that occurred afterwards instead of die()ing if you do something like this:
public function test()
{
$arrayExceptions = array();
try {
//action 1 throws an exception, as simulated below
throw new Exception('Division by zero.');
}
catch (Exception $e)
{
//handle action 1 's error using a default or fallback value
$arrayExceptions[] = $e;
}
try {
//action 2 throws another exception, as simulated below
throw new Exception('Value is not 42!');
}
catch (Exception $e)
{
//handle action 2 's error using a default or fallback value
$arrayExceptions[] = $e;
}
echo 'Task ended. Errors: '; // all the occurred exceptions are in the array
(count($arrayExceptions)!=0) ? print_r($arrayExceptions) : echo 'no error.';
}

Cleanest way to execute code outside of try block only if no exception is thrown

This question is about the best way to execute code outside of try block only if no exception is thrown.
try {
//experiment
//can't put code after experiment because I don't want a possible exception from this code to be caught by the following catch. It needs to bubble.
} catch(Exception $explosion) {
//contain the blast
} finally {
//cleanup
//this is not the answer since it executes even if an exception occured
//finally will be available in php 5.5
} else {
//code to be executed only if no exception was thrown
//but no try ... else block exists in php
}
This is method suggested by #webbiedave in response to the question php try .. else. I find it unsatisfactory because of the use of the extra $caught variable.
$caught = false;
try {
// something
} catch (Exception $e) {
$caught = true;
}
if (!$caught) {
}
So what is a better (or the best) way to accomplish this without the need for an extra variable?
One possibility is to put the try block in a method, and return false if an exception is cought.
function myFunction() {
try {
// Code that throws an exception
} catch(Exception $e) {
return false;
}
return true;
}
Have your catch block exit the function or (re)throw/throw an exception. You can filter your exceptions as well. So if your other code also throws an exception you can catch that and (re)throw it. Remember that:
Execution continues if no exception is caught.
If an exception happens and is caught and not (re)throw or a new one throw.
You don't exit your function from the catch block.
It's always a good idea to (re)throw any exception that you don't handle.
We should always be explicit in our exception handling. Meaning if you catch exceptions check the error that we can handle anything else should be (re)throw(n)
The way I would handle your situation would be to (re)throw the exception from the second statement.
try {
$this->throwExceptionA();
$this->throwExceptionB();
} catch (Exception $e) {
if($e->getMessage() == "ExceptionA Message") {
//Do handle code
} elseif($e->getMessage() == "ExceptionB Message") {
//Do other clean-up
throw $e;
} else {
//We should always do this or we will create bugs that elude us because
//we are catching exception that we are not handling
throw $e;
}
}

Categories