1 / 37

MIT AITI 2003 Lecture14 Exceptions

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

penny
Télécharger la présentation

MIT AITI 2003 Lecture14 Exceptions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIT AITI 2003 Lecture14Exceptions Java’s Error handling mechanism

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

  3. 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]); } }

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

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

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

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

  8. 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?

  9. 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"

  10. Catching Exceptions in a try-catch Block

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

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

  13. 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 }

  14. Catching Several Different Exceptions

  15. 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"); } } } }

  16. Handling Something After an Exception

  17. 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 }

  18. Throwing Exceptions

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

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

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

  22. Create your own exception class

  23. 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 extendjava.lang.Exception. public class DataFormatException   extends java.lang.Exception {     public DataFormatException() { super(); }     public DataFormatException(String s)     { super( s ); } }

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

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

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

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

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

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

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

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

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

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

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

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

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

  37. The Output… Oops you cant add the arrays Wrong!

More Related