+ All Categories
Home > Documents > MIT AITI 2003 Lecture14 Exceptions

MIT AITI 2003 Lecture14 Exceptions

Date post: 19-Jan-2016
Category:
Upload: penny
View: 42 times
Download: 0 times
Share this document with a friend
Description:
MIT AITI 2003 Lecture14 Exceptions. Java’s 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 - PowerPoint PPT Presentation
37
MIT AITI 2003 Lecture14 Exceptions Java’s Error handling mechanism
Transcript
Page 1: MIT AITI 2003 Lecture14 Exceptions

MIT AITI 2003 Lecture14Exceptions

Java’s Error handling mechanism

Page 2: MIT AITI 2003 Lecture14 Exceptions

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

Page 3: MIT AITI 2003 Lecture14 Exceptions

SampleProgram (1) -- What’s 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]);

}

}

Page 4: MIT AITI 2003 Lecture14 Exceptions

SampleProgram (2)--The output

java.lang.ArrayIndexOutOfBoundsException

at SampleProgram.main(SampleProgram.java:5)

Exception in thread "main"

Page 5: MIT AITI 2003 Lecture14 Exceptions

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 isn’t within the array’s boundaries.

• ArrayIndexOutOfBoundException is a subclass of Exception

Page 6: MIT AITI 2003 Lecture14 Exceptions

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 can’t 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

Page 7: MIT AITI 2003 Lecture14 Exceptions

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); }}

Page 8: MIT AITI 2003 Lecture14 Exceptions

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?

Page 9: MIT AITI 2003 Lecture14 Exceptions

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"

Page 10: MIT AITI 2003 Lecture14 Exceptions

Catching Exceptions in a try-catch Block

Page 11: MIT AITI 2003 Lecture14 Exceptions

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); }}

Page 12: MIT AITI 2003 Lecture14 Exceptions

NewSumNumbers – (2)

Java NewSumNumbers 1 3 5x

Output:

5x is not a number.

Those numbers add up to 4.0

Page 13: MIT AITI 2003 Lecture14 Exceptions

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

}

Page 14: MIT AITI 2003 Lecture14 Exceptions

Catching Several Different Exceptions

Page 15: MIT AITI 2003 Lecture14 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"); } } }}

Page 16: MIT AITI 2003 Lecture14 Exceptions

Handling Something After an Exception

Page 17: MIT AITI 2003 Lecture14 Exceptions

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

}

Page 18: MIT AITI 2003 Lecture14 Exceptions

Throwing Exceptions

Page 19: MIT AITI 2003 Lecture14 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.

Page 20: MIT AITI 2003 Lecture14 Exceptions

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

Page 21: MIT AITI 2003 Lecture14 Exceptions

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); }

}

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

Page 22: MIT AITI 2003 Lecture14 Exceptions

Create your own exception class

Page 23: MIT AITI 2003 Lecture14 Exceptions

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 ); }}

Page 24: MIT AITI 2003 Lecture14 Exceptions

Exception Objects -Structure• 2 Constructors

– the default constructor with no argument– the constructor that takes a string (error

message) argument

• getMessage() methodreturn the error message string

(the argument of the 2nd constructor)

Page 25: MIT AITI 2003 Lecture14 Exceptions

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();

Page 26: MIT AITI 2003 Lecture14 Exceptions

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.

Page 27: MIT AITI 2003 Lecture14 Exceptions

27

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;   }

}

Page 28: MIT AITI 2003 Lecture14 Exceptions

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.

Page 29: MIT AITI 2003 Lecture14 Exceptions

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).

Page 30: MIT AITI 2003 Lecture14 Exceptions

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

Page 31: MIT AITI 2003 Lecture14 Exceptions

Expecting the unexpectedtypes 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 Java’s exception handling mechanism.

Page 32: MIT AITI 2003 Lecture14 Exceptions

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 don’t match the signature of the methods.

Page 33: MIT AITI 2003 Lecture14 Exceptions

What’s 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;

}

Page 34: MIT AITI 2003 Lecture14 Exceptions

Let’s 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();

}}

Page 35: MIT AITI 2003 Lecture14 Exceptions

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;

}}

Page 36: MIT AITI 2003 Lecture14 Exceptions

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());}

Page 37: MIT AITI 2003 Lecture14 Exceptions

The Output…

Oops you cant add the arrays

Wrong!


Recommended