
|
View Full Version : PHP5 - exceptions
Azavia 08-31-2006, 09:48 PM Hi,
I'm still getting used to developing in PHP5, with the new features. I have experience with these features in other languages, but not in PHP.
I am curious how you handle exceptions. Do you create several exception types for various types of errors, or just always use Exception for all exceptions?
I was thinking of having a few types of exceptions, such as ArgumentException, for example. Also, I'm not sure in some cases if it requires another type of exception or not. For instance, I have a Collection class that encapsolates an array in an object-oriented form. Some of the methods accept an index, and an exception is thrown if an index is given that is out of range. I'm not sure if I should have an OutOfBoundsException or some other type, or just a more general type of Exception.
Thanks for any help. :)
123finder.com 08-31-2006, 11:57 PM You could extend the built-in Exception class and the usage is pretty similar to other languages (try...catch...). Check out the example given on the documentation page:
http://www.php.net/exceptions
Elliot A 09-01-2006, 05:10 AM You could extend the base Exception class with ones of your own, with the idea of being able to catch the right exception for the error that has occured.
For example:
<?php
try {
// Some code in here. If it failed it would throw either an out of bounds or aurguement exception
} catch ( ArgumentException $ex ) {
// Handle this exception
} catch ( OutOfBoundsException $ex ) {
// Handle this exception
} catch ( Exception $ex ) {
// This one becomes a sort of catch all
}
?>
Azavia 09-01-2006, 06:08 AM Hi,
I know I'm capable of extending exceptions, I'm just not so sure when I should do so. It would seem silly to create a new exception for every little type of error, but it also seems silly not to create any of your own type at all. So the question is, where do you draw the line, and for which type of errors do you extend Exception?
Thanks for the replies.
discobean 09-01-2006, 08:40 PM The best time to use different Exceptions is when you actually want to differentiate between errors in different places of your system.
Say in some code you have some SQL running, then a standard error. You might have an SQL Exception which you handle differently to a general error.
All in you don't get too pedantic and only implement different exceptions when you find the need arise. A good description is worth more than 1000 different Exception types.
Azavia 09-01-2006, 09:28 PM True. Do you have a set of common exception types that you generally use?
Thanks for the reply. :)
Czaries 09-02-2006, 06:16 AM I'm not sure if it's the "politically correct" way to do thing, but I am with you. I have one general exception class that I've named 'AppException' to throw all my errors. To differentiate between different errors, I just use different class constants for different error codes. Here's an example:
throw new AppException("Requested component was not found in application!<br />Requested: '".$component."'", AppException::E_404);
or:
throw new AppException('You are not authorized to view this page.', AppException::E_AUTH);
What you ultimately use is up to you, but I personally hate repetitive code. Making 10+ exceptions that are all essentially the same doesn't make any sense to me. It's too much work and too much repetitive typing (or saving File > Save As...). You may have 2-3 exception classes if the use varies a whole lot (like a DbException or a ViewException), but if it doesn't, don't overcomplicate it. Stick to the KISS principle.
Saeven 09-02-2006, 06:04 PM Using a global AppException is extremely bad practice. You should follow standard practice and use the existing exceptions in any programming language when they exist, and create your own error-specific exceptions when they don't. Much is the case with PHP which only provides you with 'Exception' - Java is a much better model for stock-exception reuse.
Instead of throwing an AppException( 'Value was not found' ); Use a ValueNotFoundException and provide specifics. Doing otherwise is just shooting yourself in the foot.
Czaries 09-02-2006, 10:19 PM Instead of throwing an AppException( 'Value was not found' ); Use a ValueNotFoundException and provide specifics. Doing otherwise is just shooting yourself in the foot.
How and why? Tell me why I should create 20 different exception classes all with pretty much the same code just to be able to type out 'DescriptionOfMyErrorExpection'. It's repetitive and annoying. I believe the Exception class has a field for an error code for a reason. I could be wrong, so please convince me. I just don't see the point of having all that repetitive code.
Elliot A 09-02-2006, 11:10 PM It wouldn't be all that repetitive. Just do something like:
class ValueNotFoundException extends Exception {}
class OutOfRangeException extends Exception {}
// ... etc
Saeven 09-03-2006, 12:22 AM How and why? Tell me why I should create 20 different exception classes all with pretty much the same code just to be able to type out 'DescriptionOfMyErrorExpection'.I think that you don't understand exceptions. What if a single method, thread or execution pattern can throw many different exceptions? What are you going to do, place a switch inside your 'global' catch block? What if your program doesn't handle a certain type of exception at a given point?
example:
try{
//doSomething
}
catch( IllegalFormatException $x1 ){
}
catch( NullPointerException $x2 ){
}
If your 'doSomething' later down the development line throws a 'BadArgumentException' which above is not caught, then your program will let you know it wasn't handled properly.
Text strings should NEVER determine exception types.
An exception is _not_ an error. It is a calculated mistake which must be handled. If all you want to do is exit from a current programmatical thread, write a method and return from it when execution should cease because of some mistake. Exceptions are _not_ control flow operands.
|