Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to catch an Exception without having to create a variable?

In PHP, I sometimes catch some exceptions with try/catch :

try {     ... } catch (Exception $e) {     // Nothing, this is normal } 

With that kind of code, I end up with the variable $e that is created for nothing (lots of resources), and PHP_MD (PHP Mess Detector) creates a warning because of an unused variable.

like image 943
Matthieu Napoli Avatar asked Jan 27 '11 18:01

Matthieu Napoli


People also ask

Can we use catch () without passing arguments in it?

Some exception can not be catch(Exception) catched. Below excecption in mono on linux, should catch without parameter. Otherwise runtime will ignore catch(Exception) statment.

Can you catch exception without try?

throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.

Can we use catch without passing arguments in Java?

You can also omit the arguments on the catch block entirely. In this case, the catch block will catch all exceptions, regardless of their type. Because you don't declare an exception variable, however, you won't have access to information about the exception.

What can I use instead of try-catch in Java?

But anyhow, even without them, Vavr Try is a real alternative for Java try-catch blocks if you want to write more functional-style code.


2 Answers

Starting with PHP 8, it is possible to use a non-capturing catch.

This is the relevant RFC, which was voted favourably 48-1.

Now it will be possible to do something like this:

try {     readFile($file); } catch (FileDoesNotExist) {     echo "File does not exist"; } catch (UnauthorizedAccess) {     echo "User does not have the appropriate permissions to access the file";     log("User attempted to access $file"); }  

With this, for some edge cases where the exception details are not relevant and exception type already provides all the necessary context, it will be possible to catch the exception without creating a new variable.

like image 52
yivi Avatar answered Sep 21 '22 07:09

yivi


You can with PHP 8 @see

PHP 5,7

No, but you can unset it.

try {     ... } catch (Exception $e) {     // Nothing, this is normal     unset($e); } 

If it is PHPMD causing this issue then you can suppress the warning.

PHPMD suppress-warnings

class Bar {     /**      * This will suppress UnusedLocalVariable      * warnings in this method      *      * @SuppressWarnings(PHPMD.UnusedLocalVariable)      */     public function foo() {          try {             ...         } catch (Exception $e) {             // Nothing, this is normal             unset($e);         }     } } 

I'm assuming you are only catching the exception because you need to not because you want to. With PHP 5,7 you have to use a catch if you want to use try and if you use a catch you have to declare a variable.

like image 39
TarranJones Avatar answered Sep 20 '22 07:09

TarranJones