By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,065 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

An Introduction to Exceptions - Ch. 2

RedSon
Expert 5K+
P: 5,000
Chapter 2: How to handle Exceptions
When you call a program, sometimes you get a message about an Unhandled exception type. This means, that something throws (or might throw) an Exception, but the compiler doesn't know, what to do then.

You can deal with this in two different ways:
  1. Throw the Exception further (pass it on to some other place)
  2. Catch the Exception and cope with it
Depending on what you want to do, when an Exception occurs, one or the other Method might be the best choice.

If you just want to throw the Exception further, it's enough to add throws Exception to any function, which should pass it on. That's the easy way and often, it is enough.
Example:
Expand|Select|Wrap|Line Numbers
  1. public void calculate(double a, double b, char func) throws Exception
  2. {
  3.     double result;
  4.     switch(func)
  5.     {
  6.     case '+' : result = add(a,b); break;
  7.     case '-' : result = sub(a,b); break;
  8.     case '*' : result = mul(a,b); break;
  9.     case '/' : result = div(a,b); break;
  10.     default  : result = 0;
  11.     }
  12.     System.out.println(a + " " + func + " " + b + " = " + result);
  13. }
  14. public double add(double a, double b)
  15. {
  16.     return a+b;
  17. }
  18. public double sub(double a, double b)
  19. {
  20.     return a-b;
  21. }
  22. public double mul(double a, double b)
  23. {
  24.     return a*b;
  25. }
  26. public double div(double a, double b) throws Exception
  27. {
  28.     if (b==0) throw new Exception();
  29.     return a/b;
  30. }
  31.  
This will try to calculate the result of a call like calculate(2.,3.,'+') or calculate(2.,3.,'/'). However, theese calles will again cause an Unhandled exception type Exception message by the compiler.

At some point the exception has to be caught and delt with. Exactly this is done by the try-catch-structure:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.     calculate(6.,0.,'/');
  4. }
  5. catch(Exception e)
  6. {
  7.     System.out.println("ERROR: You can't divide by zero!");
  8. }
  9.  
So, the program tries to calculate the result and can catch an Exception and react to it.

Now, there are different types of Exceptions and you might want to react to them in different ways. The easiest way of demonstrating this, is the following program:
Expand|Select|Wrap|Line Numbers
  1. public static void Thrower(boolean x) throws NullPointerException, ArrayIndexOutOfBoundsException 
  2.     {
  3.         if(x) throw new NullPointerException();
  4.         else throw new ArrayIndexOutOfBoundsException();
  5.     }
  6. public static void main(String[] args) {
  7.         try
  8.         {
  9.             Thrower(false);
  10.         }
  11.         catch(NullPointerException npe)
  12.         {
  13.             System.out.println("Caught a NullPointerException");
  14.         }
  15.         catch(ArrayIndexOutOfBoundsException  aioobe)
  16.         {
  17.             System.out.println("Caught an ArrayIndexOutOfBoundsException ");
  18.         }
  19.     }
  20.  
WARNING: You should NOT write a program like this in general. A function should NOT do nothing but throw Exceptions. That can create very ugly code!

Tip:
If at some point you get an Exception and want to know more about it (like where it comes from), there is e very easy method of doing so:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. catch(Exception e)
  3. {
  4.     e.printStackTrace();
  5. }
  6.  
It could give you something like this:
Expand|Select|Wrap|Line Numbers
  1. java.lang.NullPointerException
  2.     at exceptionArticle.Division.Thrower(Division.java:63)
  3.     at exceptionArticle.Division.main(Division.java:14)
Back to chapter 1 or Continue to chapter 3
Dec 18 '07 #1
Share this Article
Share on Google+