tags:

views:

89

answers:

4

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

+5  A: 

Sure, just catch the exception where you want to continue execution...

  try
  {
      SomeOperation();
  }
  catch (SomeException $e)
  {
      // do nothing... php will ignore and continue    
  }

Of course this has the problem of silently dropping what could be a very important error. SomeOperation() may fail causing other subtle, difficult to figure out problems, but you would never know if you silently drop the exception.

Doug T.
Gah! Beat me by 21 seconds.
Dominic Rodger
If i might add: catching an exception without doing anything in the catch block is considered bad style, you should at least write some log output (or, as in the example, provide a comment that _really, really, really_ nothing has to be done). This is especially true if you catch _any_ exception with catch(Exception $ex) {}
dbemerlin
+1  A: 

Sure:

try {
   throw new Exception('Something bad');
} catch (Exception $e) {
    // Do nothing
}

You might want to go have a read of the PHP documentation on Exceptions.

Dominic Rodger
+1 for the suggestion to read the manual.
GZipp
+1  A: 

Yes.

try {
    Somecode();
catch (Exception e) {
    // handle or ignore exception here. 
}

however note that php also has error codes separate from exceptions, a legacy holdover from before php had oop primitives. Most library builtins still raise error codes, not exceptions. To ignore an error code call the function prefixed with @:

@myfunction();
Crast
+2  A: 

Yes but it depends what you want to execute:

E.g.

try {
   a();
   b();
}
catch($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.

Felix Kling
If I had any votes left i'd put it on this one... :)
Peter Lindqvist