I know this is a weird on, but in my code, i have development mode errors, and production mode errors. This is the function i have:
private function error($message, $mysql_error = null){
if( DEVELOPMENT_MODE ){
$exp = new Exception();
$trace = $exp -> getTrace();
array_shift( $trace ); // removes this functions from trace
$data["Error Mg"] = $message;
$data["MySQL Er"] = ( is_null ( $mysql_error ) ) ? "" : $mysql_error;
array_unshift($trace, $data );
fkill( $trace ); // formats array and then dies
}
else{
throw new Exception ( $data );
}
}
I wrote this function in my database class, so that if an error happens, I don't have to provide the check if we're in development mode or not!
So I thought I could externalise the re-usable code. However, because I'm throwing an exception from this function, I'm basically just using a function, that will return a thrown error. Pretty useless in production mode.
I would have to do this every time i want to use it:
try{
$this -> error( "Invalid Link After Connect.", mysql_error () );
} catch ( Exception $exp ){
throw $exp;
}
RATHER THAN JUST
$this -> error( "Invalid Link After Connect.", mysql_error () );
so to avoid writing a try ... catch block for every error function I want to call... is there any way to throw the exception 2 levels up?
An exception will automatically travel up the call chain until it reaches the highest level. If it's not caught there, program execution terminates due to an uncaught exception. The whole point of exceptions is to be able to have errors bubble up. You don't need to throw harder or do anything special to "throw it up 2 levels", that's what it does by definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With