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
Related
I have a situation where I would like to re-throw an exception inside a catch block and have it caught by a more generic catch.
Example:
try {
somethingThatMightThrowExceptions();
}
catch (ClientErrorResponseException $e) {
if ($e->getResponse()->getStatusCode() == 404) {
return $this->notFound();
}
throw $e; // even tried throw new \Exception();
}
catch (\Exception $e) {
return $this->serverError('Server error');
}
So in my first catch block I check for a specific condition, and if that fails I would like to re-throw the exception and have it caught by the generic catch (\Exception) block. But in this case it just bubbles back up to the caller.
The problem is that there are actually a couple more lines of code in the final catch, which I don't want to repeat. I could of course extract them to a method, but that feels like overkill.
So basically I want to do it all in-line without having to add extra layers. Any suggestions?
It's due to catch only working once as you are throwing an exception not withing a try block.
If you are determined, that you want to do it this way, then you need to nest try..catch statements as #deceze suggested in comments..
You should describe what you are trying to accomplish instead, as there might be a better way to do it.
You have two options:
a) Extract common logic to a method. (This can be overkill sometimes as you've mentioned)
b) Catch the most generic exception and check for its type. ie:
try{
somethingThatMightThrowExceptions();
}
catch (\Exception $e) { // The most generic exception
if($e instanceof ClientErrorResponseException) {
// Some actions specific to ClientErrorResponseException
}
// Shared actions
}
You have two options:
a) Catch the most generic exception and check for its type, as #deceze suggested
try{
somethingThatMightThrowExceptions();
}
catch (\Exception $e) { // The most generic exception
if($e instanceof ClientErrorResponseException) {
// Some actions specific to ClientErrorResponseException
}
// Shared actions
}
b) Put anoter try/catch block at upper level
try {
try {
somethingThatMightThrowExceptions();
}
catch (ClientErrorResponseException $e) {
// Some actions specific to ClientErrorResponseException
throw $e ;
}
}
catch (\Exception $e) { // The most generic exception
// Shared actions
}
Hello. Is it possible to use such code in PHP?
try {
throw new InternalException('Internal');
} catch (InternalException $e) {
throw new Exception('Internal To global');
} catch (Exception $e){
print $e->getMessage();
}
class InternalException extends Exception {
// some code here
}
Nest multiple try...catch instead.
try {
throw new InternalException('Internal');
} catch (InternalException $e) {
try {
throw new Exception('Internal To global');
} catch (Exception $e){
print $e->getMessage();
}
}
class InternalException extends Exception {
// some code here
}
See PHP: Exceptions - Manual
It does not make sense to "transform" Exceptions. Don't throw em if you won't handle them.
You can catch different Exceptions this way:
try {
throw new InternalException();
} catch (HardwareException $e) {
} catch (InternalException $e) {
// this catch block will be executed
} catch (Exception $e) {
// all other exceptions
}
Yes, you can catch specific exceptions separately.
Exceptions are only caught if they're thrown in try blocks. Exceptions thrown in catch blocks are not caught within other sibling catch blocks of the same try..catch statement. You have to nest the whole thing in another outer try..catch block to catch those.
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.';
}
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;
}
}
I am trying to pass an exception from a specific catch block to a more general catch block. However it does not appear to be working. I get a 500 server error when I try the following. Is this even possible?
I realize that there are easy workarounds, but isn't it normal to say, "Hey I don't feel like dealing with this error, let's have the more general exception handler take care of it!"
try {
//some soap stuff
}
catch (SoapFault $sf) {
throw new Exception('Soap Fault');
}
catch (Exception $e) {
echo $e->getMessage();
}
Technically this is what you're looking for:
try {
try {
//some soap stuff
}
catch (SoapFault $sf) {
throw new Exception('Soap Fault');
}
}
catch (Exception $e) {
echo $e->getMessage();
}
however I agree that exceptions shouldn't be used for flow control. A better way would be like this:
function show_error($message) {
echo "Error: $message\n";
}
try {
//some soap stuff
}
catch (SoapFault $sf) {
show_error('Soap Fault');
}
catch (Exception $e) {
show_error($e->getMessage());
}