Break from try / catch block

Is this possible in PHP?

try { $obj = new Clas(); if ($obj->foo) { // how to exit from this try block? } // do other stuff here } catch(Exception $e) { } 

I know I can put other things between {} , but this increases the indentation for a larger block of code, and I don't like it: P

+9
source share
7 answers
 try { $object = new Something(); if ($object->value) { // do stuff } else { // do other stuff } } catch (Exception $e) { // handle exceptions } 
+2
source

With goto , of course!

 try { $obj = new Clas(); if ($obj->foo) { goto break_free_of_try; } // do other stuff here } catch(Exception $e) { } break_free_of_try: 
+15
source

Well, there is no reason for this, but you can enjoy the exception in your try block, stopping the execution of your function.

 try { if ($you_dont_like_something){ throw new Exception(); //No code will be executed after the exception has been thrown. } } catch (Exception $e){ echo "Something went wrong"; } 
+10
source

I also encountered this situation and, like you, I didn’t want countless if / else if / else if / else, as this makes the code less readable.

I ended the Exception class with my own. The following is an example class for validation problems that, when triggered, will give a less serious "log notification"

 class ValidationEx extends Exception { public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } public function __toString() { return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; } } 

In my main code, I call it;

 throw new ValidationEx('You maniac!'); 

Then at the end of the Try statement there is

  catch(ValidationEx $e) { echo $e->getMessage(); } catch(Exception $e){ echo $e->getMessage(); } 

Happy for comments and criticism, we are all here to learn!

+6
source

Could you do that?

 try{ $obj = new Clas(); if(!$obj->foo){ // do other stuff here } }catch(Exception $e){ } 
+3
source

In php 5.3+, the nice thing about using exceptions with a catch catch block is that you can create your own Exceptions and handle them as and when you want. See: Extending Exceptions

 class AcceptedException extends \Exception { //... } 

You can then catch specific exceptions or use if ($e instanceof AcceptedException) in your catch \Exception block to determine how you want to handle the exception (s).

 try { $obj = (object) array('foo' => 'bar'); if ($obj->foo) { throw new \AcceptedException; } } catch (\AcceptedException $e) { var_dump('I was accepted'); } catch (\Exception $e) { if ($e instanceof \InvalidArgumentException) { throw $e; //don't handle the exception } } 

This makes your code more readable and easier to troubleshoot, rather than a multitude of alternative solutions.

+1
source

Personally, I like to exit try / catch statements using

 throw new MyException("optional message", MyException::ERROR_SUCCESS); 

which I obviously catch using:

 switch($e->getCode()) { /** other cases make sense here */ case MyException::ERROR_SQL: logThis("A SQL error occurred. Details: " . $e->getMessage()); break; case MyException::ERROR_SUCCESS: logThis("Completed with success. Details: " . $e->getMessage()); break; case MyException::ERROR_UNDEFINED: default: logThis("Undefined error. Details: " . $e->getMessage()); break; } 
0
source

Source: https://habr.com/ru/post/944653/


All Articles