views:

261

answers:

4

Hi,

I have been working with Exceptions lately. I think it makes sense to log uncaught Exceptions, because it greatly helps developers to take care of possible problems in the source by simply looking at Exception log. However, when an exception is dealt with, is there a need to log it any longer? Yes, to some extent. If you have "bad" developers who write code that has bugs, then by simply catching Exceptions will make the bugs go "away". For example:

try
{
 fopen('/path/to/file','w');
}
catch (Exception $e)
{
 // Display an error to user
}

The above code is PHP. The point is, this kind of code, imo, should not exist. Exceptions are meant to be exceptional and occur rarely, but with this code they occur more than rarely. For instance, the developer does not check if the file exists and he does not check if he has permissions to write to it. Instead, it should be:

try
{
 if (file_exists('/path/to/file') && is_writable('/path/to/file'))
  fopen('/path/to/file','w');
 else
  // Display an error to user about file not existing or not being writable
}
catch (Exception $e)
{
 // Display an error to user about an unexpected error
}

This makes pretty much sense now. The exception is thrown and caught only if it is exceptional (e.g. server crash, power outage, whatever). But what if the developer uses Exceptions for everything and the omits checks - is there a way for me to log caught exceptions?

I don't want developers to put everything in try-catch blocks to "solve" issues. I want them to explicitly check whatever they should check - and leave exceptions for exceptional situations like hardware failure, server crash, etc.

+5  A: 

Exceptions are meant to be exceptional and occur rarely, but with this code they occur more than rarely.

I think that's a misconception. The point of exceptions is to separate the ideal flow from the error-handling flow.

troelskn
+1  A: 

I don't think that there's a magic method type approach if that's what you're looking for. But is it possible to wrap your entire app in a try... catch... structure? By definition, any exceptions that get to the outer-most try... catch... are uncaught... or am I missing the point ;-)

Dycey
set_exception_handler (http://www.php.net/manual/en/function.set-exception-handler.php)
troelskn
A: 

I agree with troelskn's comment that you should just embrace exceptions as a valid way to harden code.

If you still want to log the uncaught exceptions, you'd have to have all your developers pass them to a logging function (eg. log_exception($e)) or catch them all in a high-level try/catch and log them there. You could also use a tool like PHP_CodeSniffer to detect code written like your first example, but it's not a great solution. You'd need to write your own sniff to detect this case; it could look for a try block that doesn't contain any if statements.

Garret Heaton
+2  A: 

You can use PHP's set_exception_handler function, this will let you supply a function that will catch all uncaught exceptions but you'll have to add manual logging for all those that are caught in your scripts.

Mathew Hall