never_had_a_name
never_had_a_name

Reputation: 93246

exceptions thrown terminate the script?

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

Upvotes: 0

Views: 269

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163288

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.

Upvotes: 2

Related Questions