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:
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:
[CODE=java]
public void calculate(doubl e a, double b, char func) throws Exception
{
double result;
switch(func)
{
case '+' : result = add(a,b); break;
case '-' : result = sub(a,b); break;
case '*' : result = mul(a,b); break;
case '/' : result = div(a,b); break;
default : result = 0;
}
System.out.prin tln(a + " " + func + " " + b + " = " + result);
}
public double add(double a, double b)
{
return a+b;
}
public double sub(double a, double b)
{
return a-b;
}
public double mul(double a, double b)
{
return a*b;
}
public double div(double a, double b) throws Exception
{
if (b==0) throw new Exception();
return a/b;
}
[/CODE]
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:
[CODE=java]
try
{
calculate(6.,0. ,'/');
}
catch(Exception e)
{
System.out.prin tln("ERROR: You can't divide by zero!");
}
[/CODE]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:
[CODE=java]
public static void Thrower(boolean x) throws NullPointerExce ption, ArrayIndexOutOf BoundsException
{
if(x) throw new NullPointerExce ption();
else throw new ArrayIndexOutOf BoundsException ();
}
public static void main(String[] args) {
try
{
Thrower(false);
}
catch(NullPoint erException npe)
{
System.out.prin tln("Caught a NullPointerExce ption");
}
catch(ArrayInde xOutOfBoundsExc eption aioobe)
{
System.out.prin tln("Caught an ArrayIndexOutOf BoundsException ");
}
}
[/CODE]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:
[CODE=java]
...
catch(Exception e)
{
e.printStackTra ce();
}
[/CODE]It could give you something like this:
Back to chapter 1 or Continue to chapter 3
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:
- Throw the Exception further (pass it on to some other place)
- Catch the Exception and cope with it
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:
[CODE=java]
public void calculate(doubl e a, double b, char func) throws Exception
{
double result;
switch(func)
{
case '+' : result = add(a,b); break;
case '-' : result = sub(a,b); break;
case '*' : result = mul(a,b); break;
case '/' : result = div(a,b); break;
default : result = 0;
}
System.out.prin tln(a + " " + func + " " + b + " = " + result);
}
public double add(double a, double b)
{
return a+b;
}
public double sub(double a, double b)
{
return a-b;
}
public double mul(double a, double b)
{
return a*b;
}
public double div(double a, double b) throws Exception
{
if (b==0) throw new Exception();
return a/b;
}
[/CODE]
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:
[CODE=java]
try
{
calculate(6.,0. ,'/');
}
catch(Exception e)
{
System.out.prin tln("ERROR: You can't divide by zero!");
}
[/CODE]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:
[CODE=java]
public static void Thrower(boolean x) throws NullPointerExce ption, ArrayIndexOutOf BoundsException
{
if(x) throw new NullPointerExce ption();
else throw new ArrayIndexOutOf BoundsException ();
}
public static void main(String[] args) {
try
{
Thrower(false);
}
catch(NullPoint erException npe)
{
System.out.prin tln("Caught a NullPointerExce ption");
}
catch(ArrayInde xOutOfBoundsExc eption aioobe)
{
System.out.prin tln("Caught an ArrayIndexOutOf BoundsException ");
}
}
[/CODE]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:
[CODE=java]
...
catch(Exception e)
{
e.printStackTra ce();
}
[/CODE]It could give you something like this:
Code:
java.lang.NullPointerException
at exceptionArticle.Division.Thrower(Division.java:63)
at exceptionArticle.Division.main(Division.java:14)