tags:

views:

66

answers:

2

I'd like to use an exception for error handling in a part of my code but if the code should fail, I would like the script to continue. I want to log the error though. Can someone please help me figure this out?


try{
    if($id == 4)
    {
      echo'test';
    }
}
catch(Exception $e){
    echo $e->getMessage();
}

echo'Hello, you should see me...'; <------ I never see this.. No errors, just a trace.
+2  A: 

In the code that is calling the code that may throw an Exception do

try { 
    // code that may break/throw an exception
    echo 'Foo';
    throw new Exception('Nothing in this try block beyond this line');
    echo 'I am never executed';
    throw new CustomException('Neither am I');
} catch(CustomException $e) {
    // continue here when any CustomException in try block occurs
    echo $e->getMessage();
} catch(Exception $e) { 
    // continue here when any other Exception in try block occurs
    echo $e->getMessage();
}

// script continues here
echo 'done';

Output will be (adding line breaks for readability):

'Foo'                                         // echoed in try block
'Nothing in this try block beyond this line'  // echoed in Exception catch block
'done'                                        // echoed after try/catch block

Try/Catch Blocks may also be nested. See Example 2 in the PHP Manual page linked above:

try{
    try {
        throw new Exception('Foo');
        echo 'not getting here';
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    echo 'bar';
} catch (Exception $e) {
    echo $e->getMessage();
}
echo 'done';

'Foo'  // echoed in inner catch block
'bar'  // echoed after inner try/catch block
'done' // echoed after outer try/catch block

Further reading at DevZone:

Gordon
The script inside the try statement will not continue to execute.
Chacha102
Excellent Gordon, thank you. I have this much already. What happens though is when there is an error (TCP/IP), the script dies. What I would like to do, because these TCP/IP errors generally correct themselves quickly is just have the script continue. However, I would like to log the error. Is this at all possible using exceptions?
Bebo
I'm not sure I understand what you are refering to with TCP/IP errors. If you mean the connection to the server dies and you want to pick it up on the next request, then no. This is impossible.
Gordon
Well, if the connection dies on the first time out, log the error but continue. If it fails again, the die. Possible?
Bebo
I've edited my example. Maybe it gets clearer from this.
Gordon
Thank you Gordon. This helps. This also looks like what I posted up top where I was trying to throw an exception on a second error. It doesn't look like it's possible though.
Bebo
You can catch various types of Exceptions in a try block, but once an Exception is catched, the try block will not be continued.
Gordon
Ok, and that is concrete, right? I mean, when it is said that an error can be "re-thrown", it doesn't mean that the same try block will be tried a second time. Right?
Bebo
You guys have been great help. GOrdon, I'm going to give you the credit on this one because you've included the custom exception and I believe this is what I am after. I'd like to ask you a few more questions but it looks like your gone. Thanks for your help.
Bebo
"Re-Thrown" in the example only means the catched exception from the inner block is thrown again and then gets catched by the outer block. If it wasnt thrown in the catch block, program flow would continue after the inner try/catch block. This might give you the level of granularity you are looking for, though I find it mind boggling to follow.
Gordon
+4  A: 

You have to catch the exception :

// some code

try {
    // some code, that might throw an exception
    // Note that, when the exception is thrown, the code that's after what
    // threw it, until the end of this "try" block, will not be executed
} catch (Exception $e) {
    // deal with the exception
    // code that will be executed only when an exception is thrown
    echo $e->getMessage(); // for instance
}

// some code, that will always be executed


And here are a couple of things you should read :

Pascal MARTIN
The code inside the try statement will not continue to execute.
Chacha102
@Chacha102 : I've edited my answer to insist on that -- the solution being to put the code "that must always be excuted" after the try/catch block ;; still, this offers the ability to log the exception, and the script will continue.
Pascal MARTIN
Thanks Pascal, This much I already have down. I would like to log the first error and have the script continue, if possible. Is this possible?
Bebo
@Pascal, I don't believe you understand the question. He wants the block inside the try statement to continue to execute.
Chacha102
@Bebo : the script's excution will continue after the try/catch block -- which means the try block should contain a group of statements that have to be executed as a whole (I'm thinking about a database transaction, for example)
Pascal MARTIN
Oh wow.. So the code that gets executed goes below the catch block.. Right?
Bebo
@Pascal, I realize this is not possible, but I am simply waiting for someone to mention that :)
Chacha102
@Chacha102 : oh, I didn't see the question like this, I have to admit ;; @Bebo : well, that's just not the way exceptions really work ^^ ;; and yes, the code after the catch block will always be excuted (independantly of whether there's been an exception thrown or not)
Pascal MARTIN
Hmm. I have the same exact code that you have posted but my code dies with the errors logged as expected. Nothing below my catch block executes.
Bebo
@Pascal, Of course now I'm going to look like an idiot if I am actually wrong and it was the way you perceived it.
Chacha102
@Bebo : that's odd ; are you sure your code is throwing an exception (and not an "error") ; and are you sure there's no `exit` nor `die` ?
Pascal MARTIN
Yes, 100% positive. The code throws an exception because I get the trace along with it. This was why I was wondering how to use or better phrased, why to use exceptions if they are going to kill your script.
Bebo
Yes, Pascal, I only use die in my db script and it is with the mysql error attached. There is no exception in that script.
Bebo
as long as your exception is catched, and you are not using exit not die in the catch block (nor have anything that raises another problem), there is no reason for your script to end... Could you edit your question, to show us more relevant code, so we can understand better ?
Pascal MARTIN
Sure. Pascal. I will post my exception block too.
Bebo
I had to change my downvote ... so I edited your answer, btw
Chacha102