You are on page 1of 37

MIT AITI 2003 Lecture14 Exceptions

Javas Error handling mechanism

Objectives
(we will see if we have achieved them at the end of the day)

How to use methods that cause exceptions How to respond to exceptions in your Java programs How to create methods that ignore an exception, leaving it for another class to handle How to create your own exceptions

SampleProgram (1)
-- Whats the output of the following program?

public class SampleProgram { public static void main(String args[]) { String[] greek = {"Alpha", "Beta", "Gamma"}; System.out.println(greek[3]); } }

SampleProgram (2)
--The output
java.lang.ArrayIndexOutOfBoundsException at SampleProgram.main(SampleProgram.java:5) Exception in thread "main"

SampleProgram (3)
--Notes
Compiles successfully but encounters a problem when it runs The Java interpreter made note of the exception by displaying the error message and stopped the program. An object of type ArrayIndexOutOfBoundException is created to alert the user you have used an array element that isnt within the arrays boundaries. ArrayIndexOutOfBoundException is a subclass of Exception

Some words about Exceptions


All exceptions are subclasses of Exception. Some exceptions are comparable to compiler errors, e.g. ArrayIndexOutOfBoundsException Others must be dealt with every time a program runs you cant deal with or you want to handle them within a Java class Exception handling is done using these five statements: try, catch, finally, throw and throws

SumNumbers (1)
public class SumNumbers { public static void main(String[] arguments) { float sum = 0; for (int i=0; i<arguments.length; i++) sum += Float.parseFloat(arguments[i]); System.out.println("Those numbers add up to "+ sum); } }

SumNumbers (2)
java SumNumbers 8 6 7 5 3 0 9 Output: Those numbers add up to 38.0
Java SumNumbers 1 3 5x Output?

SumNumbers (3)
java.lang.NumberFormatException: 5x at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1176) at java.lang.Float.parseFloat(Float.java:183) at SumNumbers.main(SumNumbers.java:7) Exception in thread "main"

Catching Exceptions in a trycatch Block

NewSumNumbers (1)
public class NewSumNumbers { public static void main(String[] arguments) { float sum = 0; for (int i=0; i<arguments.length; i++) try { sum += Float.parseFloat(arguments[i]); } catch (NumberFormatException e) { System.out.println(arguments[i] + " is not a number."); }
System.out.println("Those numbers add up to "+ sum); }

NewSumNumbers (2)
Java NewSumNumbers 1 3 5x
Output: 5x is not a number. Those numbers add up to 4.0

NewSumNumbers (3)
A try-catch block can be used with any exception that you want a program to handle
try{ //statement(s) that might cause the exception } catch (Exception e) { //what to do when the exception occurs }

Catching Several Different Exceptions

public class DivideNumbers { public static void main(String[] arguments) { if (arguments.length == 2) { int result =0; try { result = Integer.parseInt(arguments[0])/Integer.parseInt(arguments[1]); System.out.println(arguments[0] + " divided by " + arguments[1] + " equals " + result); } catch (NumberFormatException e) { System.out.println("Both arguments must be numbers."); } catch (ArithmeticException e) { System.out.println("You cannot divide by zero"); } } } }

Handling Something After an Exception

try-catch-finally block
try{ //statement(s) that might cause the exception } catch (Exception e) { //what to do when the exception occurs } finally { //statements to execute no matter what }

Throwing Exceptions

When a method throws an exception


NetReader.java:14: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown This indicates that you are trying to use a method that throws an exception. You must do one of the following things:
Handle the exception with a try-catch block Throw the exception Handle the exception with a try-catch block and then throw it.

Handle the exception with a try-catch block


public class NewSumNumbers {

public static void main(String[] arguments) {


float sum = 0; for (int i=0; i<arguments.length; i++) try {

sum += Float.parseFloat(arguments[i]);
} catch (NumberFormatException e) { System.out.println(arguments[i] + " is not a number."); } System.out.println("Those numbers add up to "+ sum); } }

Handle the exception with a try-catch block and then throw it


public class NewSumNumbers {

public static void main(String[] arguments) throws NumberFormatException {


float sum = 0; for (int i=0; i<arguments.length; i++) try {

sum += Float.parseFloat(arguments[i]);
} catch (NumberFormatException e) { System.out.println(arguments[i] + " is not a number."); throw e; } System.out.println("Those numbers add up to "+ sum); } }

Create your own exception class

Writing Your Own Exception Classes


Writing your own exception class is simple. New exception classes allow you to handle a new type of error separately. Exception classes extend java.lang.Exception.
public class DataFormatException extends java.lang.Exception { public DataFormatException() { super(); } public DataFormatException(String s) { super( s ); } }

Exception Objects -Structure


2 Constructors
the default constructor with no argument the constructor that takes a string (error message) argument

getMessage() method
return the error message string (the argument of the 2nd constructor)

Creating and Throwing your own Exception instances


Exceptions are objects. You must create a new instance of an exception before you can throw it
if ( dArray.length == 0 ) throw new IllegalArgumentException();

Declaring an Exception
If a method can throw an exception, you can always declare the type of the exception in the header after the keyword throws
public static double average( double [] dArray ) throws IllegalArgumentException

The compiler requires you to declare the possible exception throw if the exception class is not derived from RuntimeException or Error. These are called checked exceptions (checked by the compiler). Exceptions derived from RuntimeException are called unchecked exceptions and their declaration is optional.

Throwing an Exception
-- An Example
public static double average( double [] dArray ) throws IllegalArgumentException { if ( dArray.length == 0 ) throw new IllegalArgumentException(); else { double sum = 0.0; for ( int i = 0; i < dArray.length; i++ ) sum += dArray[ i ]; return sum / dArray.length; } }
27

Exceptions, Errors, and RuntimeExceptions


Throwable have two special subclasses: Error and Exception Error: a catastrophic failure (your program will not be able to recover) Exception: a non-catastrophic failure (your program should recover or exit gracefully) RuntimeException (unchecked Exception): a special subclass of Exception. Methods and constructors throwing RuntimeExceptions do not have to declare this fact. All other exceptions (checked exception): methods or constructors throwing checked exception should declare this fact.

Checked vs. Unchecked Exceptions


In principle, checked exceptions are those which you, the programmer, are supposed to be able to fix at runtime, such as a FileNotFoundException. Very often these are generated in system code by user, not programmer error. Unchecked exceptions (RuntimeExceptions) are supposed to be able to occur anywhere (so hard to check for) and to be the result of programmer error (so the best way of handling them is to fix the program).
Good examples of unchecked exceptions: NullPointerException, ArrayIndexOutOfBoundsException. Bad example of unchecked exceptions, NumberFormatException, thrown e.g. by Float.parseFloat(String s).

Recap: When things go wrong


What happens when things go wrong? How do I create alternative ways to handle atypical circumstances? Do you know the followings?
How to use methods that cause exceptions How to respond to exceptions in your Java programs How to create methods that ignore an exception, leaving it for another class to handle How to create your own exceptions

Expecting the unexpected


types of Unexpected Circumstances

Catastrophic failures: can not be prevented, but certain systems need to design in mechanisms to minimize the damage that they cause Some failures: can be anticipated and avoided through simple checks and guards. Other failures: must be handled as they arise, often use Javas exception handling mechanism.

How would you classify these situations?


Someone tripped over the power cord of the computer on which your program was running. Divisor is 0 for a division operation. The arguments dont match the signature of the methods.

Whats wrong here?


public static int [] add(int [] n1, int [] n2) { int [] n3 = new int[n1.length]; for (int k = 0; k<n1.length; k++) n3[k] = n1[k]+n2[k]; return n3; }

Lets write an exception to handle the problem


public class MyArrayException extends Exception{ public MyArrayException() {super();} public MyArrayException(String s) {super(s);}
public String getMessage(){ System.out.println(Oops you cant add the arrays); return super.getMessage(); }

Now, rewrite the function


public static int [] add(int [] n1, int [] n2) throws MyArrayException { if (n1.length ! = n2.length) throw new MyArrayException(Wrong!); else { int [] n3 = new int[n1.length]; for (int k = 0; k<n1.length; k++) n3[k] = n1[k]+n2[k]; return n3; } }

So, what will happen here?


int [] a = { 3, 5, 6}; int [] b = { 4, 9}; try { int [] c = MyArrayTest.add(a, b); } catch (MyArrayException e) { System.out.println(e.getMessage()); }

The Output
Oops you cant add the arrays Wrong!

You might also like