5

I admit I do not use Exceptions a whole lot and they are at time hard for me to grasp 100% in PHP, this could be partially because PHP does not have the best error => Exceptions support but none the less I do not know much about them.

Take for example this code below, it has 4 different Classes defined that do nothing but extend a base Exception class. I am just curious why one would not just call an Exception and why they have all these separate classes.

I assume there is a good reason?

class OptimizeImageException extends Exception {};

class FileNotFoundException extends OptimizeImageException {};

class FileNotImageException extends OptimizeImageException {};

class ModuleNotFoundException extends OptimizeImageException {};
7
  • Specificity is the only reason I can think of; if the exception has a name, at least maybe you might know where to start when it starts showing up. Commented Jan 9, 2012 at 1:42
  • @Jared Farrish: "when it starts showing up" - you have exception trace and know where exactly it has been thrown, with or without specific class for each case Commented Jan 9, 2012 at 1:46
  • @zerkms - I'm not sure if you're agreeing or disagreeing with me? Commented Jan 9, 2012 at 1:48
  • @Jared Farrish: I disagree with you: "if the exception has a name, at least maybe you might know where to start when it starts showing up" --- you can always get where the exception came from even without its name, because each exception has stack trace Commented Jan 9, 2012 at 1:49
  • 2
    @zerkms - Maybe we interpret what "where to start" as different. Your answer is pretty much what I meant. That's why I stated "specificity", which did not mean to be "line-specific" (which, personally, I think is weird to assume). Commented Jan 9, 2012 at 1:51

2 Answers 2

7

By having multiple Exception classes, you can pick out which one you're interested in when catching them.

<?php                                                                       
class OptimizeImageException extends Exception {};

class FileNotFoundException extends OptimizeImageException {};

class FileNotImageException extends OptimizeImageException {};

class ModuleNotFoundException extends OptimizeImageException {};

try {
  throw new FileNotImageException();
} catch (FileNotFoundException $x) {
  echo "NOT FOUND!";
  // do something about it
} catch (FileNotImageException $x) {
  echo "NOT IMAGE!";
  // do something about it
} catch (Exception $x) {
  echo "UNKNOWN EXCEPTION!";
  // do something else about it
}

This is a trivial example, but say you have a function loadImage() which is supposed to load an image. If the function fails, you can handle different failure scenarios differently. If you always throw a basic Exception, you only know something went wrong. You don't know what went wrong, so you can't have different recovery responses based on different scenarios, not without using another mechanism (which then makes exceptions rather weak).

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

Comments

3

You need to have different exception classes to be able to find out what actually happened in case that some particular exception could be handled.

Like:

try {
    // do something
} catch (OptimizeImageException $e) {
    // image cannot be optimized. left it as is and log the error message
}

In the code above you're handling one particular exception case, that can be processed right here and right now to move your application flow as expected. Other possible exceptions will go upper.

If you had only one exception class like MyAppException then all you could do is just catch it, check the message (which is weird) to realize what actually happened and if you cannot handle it - rethrow exception. As you can see this way your code would be a bit hackish and unmaintainable (you cannot change exception message)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.