tags:

views:

26

answers:

1

i wonder if exceptions that are thrown in php will terminate the script in php?

cause when i save an entry that is already created in doctrine it throws an exception.

i catch the exception and ignore it (so that the user won't see it) but the script seems to be terminated.

is there a way to catch the exception and keep the script alive?

thanks

+2  A: 

You need to wrap the function call(s) that may throw an exception in a try...catch block.

class EvilException extends Exception {}
class BadException extends Exception {}

function someMethodThatMayThrowException() {
   ...
   ...
   throw new EvilException( "I am an evil exception. HAHAHAHA" );
}
try {

someMethodThatMayThrowException();

} catch( BadException $e ) {
  //deal with BadException here...
} catch( EvilException $e ) {
   //deal with EvilException here...
   throw new Exception( "will be caught in next catch block" );
} catch( Exception $e ) {
   echo $e->getMessage(); //echoes the string: "will be caught in next catch block"
}

If you catch the exception(s), the script will not terminate. If a thrown exception does not have a catch block to jump into, the aforementioned will happen.

Jacob Relkin
+1 you can also throw the exception again in the `catch` block to pass it on.
Pekka