Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: catch exception and continue execution, is it possible?

Tags:

php

Is it possible to catch exception and continue execution of script?

like image 796
Kirzilla Avatar asked Jan 25 '10 14:01

Kirzilla


People also ask

Can PHP try have multiple catches?

PHP allows a series of catch blocks following a try block to handle different exception cases. Various catch blocks may be employed to handle predefined exceptions and errors as well as user defined exceptions.

Does try catch stop execution?

The “try… First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .

Does exception catch all exceptions PHP?

Catching all PHP exceptions Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... }


1 Answers

Yes but it depends what you want to execute:

E.g.

try {    a();    b(); } catch(Exception $e){ }  c(); 

c() will always be executed. But if a() throws an exception, b() is not executed.

Only put the stuff in to the try block that is depended on each other. E.g. b depends on some result of a it makes no sense to put b after the try-catch block.

like image 89
Felix Kling Avatar answered Oct 02 '22 02:10

Felix Kling