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

php

Question

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

1
64
1/25/2010 2:10:43 PM

Accepted Answer

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.

87
1/25/2010 2:36:03 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon