6

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?

1
  • 1
    I would have to do this every time i want to use it: Why's that? Commented Mar 12, 2012 at 8:46

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

4 Comments

OH! i didnt realise... i thought they had to be caught on every level. My bad
I have to admit I was tempted to answer throw harder new Exception(). ;)
but i do need to throw harder... it doesn't bubble.
I would like to say that if you are using namespaces, you may have to do catch (\Exception $e) { rather than catch (Exception $e) {
10

Just omit the try/catch block. Exceptions automatically propagate up as far as they can until something catches them; you don't need to explicitly re-throw them at every level of the call stack.

This...

try{ $this -> error( "Invalid Link After Connect.", mysql_error () ); } catch ( Exception $exp ){ throw $exp; } 

is exactly equivalent to this:

$this -> error( "Invalid Link After Connect.", mysql_error () ); 

1 Comment

Almost exactly equivalent... getLine would return a different number. :)
1

Use Multiple catch Blocks use admin table which has field

Mode Value 0 Production 1 Debug 

the first catch which matches the exception is executed

Example

 try { if (!$bDBConnection && $row['mode'] ==0 ) { throw new Produciton_DBException("Problem with Database"); } else { throw new Debug_DBException("Problem with Database"); } } catch(Produciton_DBException $e) { // display suitable error messages } catch(Debug_DBException $ex) { // Exception falls through until a match is found } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.