Connecting Tech Pros Worldwide Help | Site Map

An Introduction to Exceptions - Ch. 4

Nepomuk's Avatar
Moderator
 
Join Date: Aug 2007
Location: Germany
Posts: 2,466
#1   Sep 19 '07
Chapter 4: How can I write my own exceptions?
Now we have seen, how you can work with the Exceptions, which are supplied and what some of the most common of them mean. However, just imagine you want to throw an Exception, which doesn't fit with any Exception there is predefined?

Example:
You have written your own text adventure (yes, they are old fashioned, but they still exist) and you want to throw an Exception, when a command is valid, but can't be used in this situation (e.g. "look at door" when it's dark).
You can use if-statements here, but let's just choose to use Exceptions in this case.
What you want now is a ActionNotPossibleNowException. You want to tell it, what can't be done and why it can't be done and it should give you an output, which covers both.
This can be coded like this:
Expand|Select|Wrap|Line Numbers
  1. public class ActionNotPossibleNowException extends Exception {
  2.     public ActionNotPossibleNowException(String action, String reason)
  3.     {
  4.         super("Could not " + action + " now. Reason: " + reason);
  5.     }
  6. }
  7.  
As you see, it's not any different to any other Superclass relationship. All we have to know, is that Exception has a constructor, which takes a String as an argument and this String is the error message.
So, now you can have a method lookAt in your character class, which would look something like this:
Expand|Select|Wrap|Line Numbers
  1. public class Character {
  2.     private boolean lightIsOn = true;
  3.  
  4.     ...
  5.  
  6.     public void lookAt(String target) throws ActionNotPossibleNowException
  7.     {
  8.         if(!lightIsOn) throw new ActionNotPossibleNowException("look at " + target, "It is dark.");
  9.         else
  10.         {
  11.             ...
  12.         }
  13.     }
  14. }
  15.  
Of course, you can extend any other given Exception as well - if you decide, that the ActionNotPossibleNowException is actually an Input-Output-Exception (IOException), simply change the code to
Expand|Select|Wrap|Line Numbers
  1. public class ActionNotPossibleNowException extends IOException {
  2.     public ActionNotPossibleNowException(String action, String reason)
  3.     {
  4.         super("Could not " + action + " now. Reason: " + reason);
  5.     }
  6. }
  7.  
Which can now be caught any of these lines:
Expand|Select|Wrap|Line Numbers
  1. catch(ActionNotPossibleNowException anpne){...}
  2. catch(IOException ioe){...}
  3. catch(Exception e){...}
  4.  
Often, you should not extend Exception directly, but a more specialized Exception. For example
Expand|Select|Wrap|Line Numbers
  1. class NoKeyPressedException extends IOException {
  2.    // ... 
  3. }
  4.  
and
Expand|Select|Wrap|Line Numbers
  1. class NoMouseButtonPressedException extends IOException {
  2.    // ... 
  3. }
  4.  
will both be caught by
Expand|Select|Wrap|Line Numbers
  1. catch(IOException ioe) {
  2.    //...
  3. }
  4.  
and you can still have a third exception
Expand|Select|Wrap|Line Numbers
  1. class ThirdException extends Exception {
  2.    //...
  3. }
  4.  
which isn't caught by that catch-block and can be handled separately. You can solve this differently, however this is a easy and sufficient way of doing so.

Back to chapter 3 or Continue to chapter 5
Attached Files
File Type: zip Chapter4.zip (715 Bytes, 76 views)



Reply