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.
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 have the following script.
According to http://php.net/manual/en/class.pdoexception.php, You should not throw a PDOException from your own code.
But I want the same catch to be performed whether a PDOException or the exception that I threw for an invalid foo.
I've also been told that I should never catch the generic Exception, but only catch specific Exceptions.
How should this be implemented?
try {
db::db()->beginTransaction();
//Do a bunch of queries, and a PDO exception will be thrown upon error
if($foo($bar)) {throw new Exception('Invalid foo.');}
db::db()->commit();
} catch (PDOException $e) {
db::db()->rollBack();
//Maybe do some other stuff
}
Something like
try {
db::db()->beginTransaction();
//Do a bunch of queries, and a PDO exception will be thrown upon error
if($foo($bar)) {throw new RuntimeException('Invalid foo.');}
db::db()->commit();
} catch (PDOException $e) {
db::db()->rollBack();
//Maybe do some other stuff
} catch (RuntimeException $e) {
//foo invalid
}
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
I have the following function:
public function getClientTable($feedUrl)
{
$client = new Zend_Http_Client($feedUrl);
try
{
return $client->request()->getBody();
}
catch (Zend_Http_Client_Adapter_Exception $e)
{
return false;
}
}
It seems to work great for catching that specific Zend_Http_Client_Adapter_Exception; but what if I want it to catch additional exceptions? Hell, what if I wanted it to catch ALL exceptions... how would I do this?
Also, should I be using "return" or "throw" in the try? Why does it matter?
You can have multiple catch statements, eg
try {
// whatever
} catch (Zend_Http_Client_Adapter_Exception $e) {
// ah ha
} catch (Zend_Some_other_Exception $e) {
// ah ha
} catch (Exception $e) {
// And the final fallback catch that grabs all exceptions
}
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());
}