1 / 58

Exceptions & exception handling

Learn how to define, create, and handle exceptions in Java effectively. Understand exception classes, instances, throwing, and handling techniques. Explore exercises to test your exception handling skills.

cedricks
Download Presentation

Exceptions & exception handling

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. Exceptions &exception handling

  2. Exceptions & exception handling • Use sparingly. • Things you can do with exceptions: • Define a new exception class. • Create an exception instance. • Throw an exception. • Declare that an exception may be thrown (in a particular function). • Handle the possibility that an exception may be thrown.

  3. Exceptions & exception handling • Use sparingly. • Things you can do with exceptions: • Define a new exception class. • Create an exception instance. • Throw an exception. • Declare that an exception may be thrown (in a particular function). • Handle the possibility that an exception may be thrown.

  4. 1. Define a new exception class • extend Exception (or extend a subclass of Exception) • See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html. • This creates a new type. • All have a ctor w/ a single String arg. • Each has an accessor method called getMessage() that returns the String from the ctor arg.

  5. Define a new exception class example //typical sample code public class DivisionByZeroException extends Exception { public DivisionByZeroException ( ) { super( “Division by zero!” ); } public DivisionByZeroException ( String message ) { super( message ); } }

  6. public class BadNumberException extends Exception { private int mBadNumber; public BadNumberException ( ) { super( “BadNumberException” ); } public BadNumberException ( String message ) { super( message ); } public BadNumberException ( int number ) { super( “BadNumberException” ); mBadNumber = number; } public int getBadNumber ( ) { return mBadNumber; } } Define a new exception class example (w/ more information)

  7. Exceptions & exception handling • Use sparingly. • Things you can do with exceptions: • Define a new exception class. • Create an exception instance. • Throw an exception. • Declare that an exception may be thrown (in a particular function). • Handle the possibility that an exception may be thrown.

  8. 2. Create an exception instance Ex. new Exception( “Uh oh!” ); Exception e = new Exception( “Rats!” );

  9. Exceptions & exception handling • Use sparingly. • Things you can do with exceptions: • Define a new exception class. • Create an exception instance. • Throw an exception. • Declare that an exception may be thrown (in a particular function). • Handle the possibility that an exception may be thrown.

  10. 3. Throw an exception Ex. throw new Exception( “Invalid value.” ); Exception e = new Exception( “Invalid age.” ); throw e;

  11. Exceptions & exception handling • Use sparingly. • Things you can do with exceptions: • Define a new exception class. • Create an exception instance. • Throw an exception. • Declare that an exception may be thrown (in a particular function). • Handle the possibility that an exception may be thrown.

  12. 4. Declare (a method that indicates) that an exception may be thrown Ex. public int f ( int x ) throws Exception { … }

  13. Exceptions & exception handling • Use sparingly. • Things you can do with exceptions: • Define a new exception class. • Create an exception instance. • Throw an exception. • Declare that an exception may be thrown (in a particular function). • Handle the possibility that an exception may be thrown.

  14. 5. Handle the possibility that an exception may be thrown • The try-catch blocks: try { … } catch (Exception e) { … }

  15. Exercises

  16. Exercises 1. What is the output produced by the following code? int waitTime = 46; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  17. Exercises 1. What is the output produced by the following code? int waitTime = 46; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” ); Try block entered. Over 30. After catch block.

  18. Exercises 2. What is the output produced by the following code? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  19. Exercises 2. What is the output produced by the following code? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” ); Try block entered. Under 30. After catch block.

  20. Exercises 3. What are the throw statements (below)? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  21. Exercises 3. What are the throw statements (below)? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  22. Exercises 4. What happens when a throw statement is executed? This is a general question. Tell what happens in general, not simply what happens in the code below or some other sample code. int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  23. Exercises 5. What is the try block (below)? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  24. Exercises 5. What is the try block (below)? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  25. Exercises 6. What is the catch block (below)? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  26. Exercises 6. What is the catch block (below)? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  27. Exercises 7. What is the catch block parameter (below)? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  28. Exercises 7. What is the catch block parameter (below)? int waitTime = 12; try { System.out.println( “Try block entered.” ); if (waitTime>30) throw new Exception( “Over 30.” ); else if (waitTime<30) throw new Exception( “Under 30.” ); else System.out.println( “No exception.” ); System.out.println( “Leaving try block.” ); } catch (Exception thrownObject) { System.out.println( thrownObject.getMessage() ); } System.out.println( “After catch block.” );

  29. Exercises • 8. Is the following legal? Exception exceptionObject = new Exception( “Oops!” );

  30. Exercises • 8. Is the following legal? Exception exceptionObject = new Exception( “Oops!” ); Yes.

  31. Exercises • 9. Is the following legal? Exception exceptionObject = new Exception( “Oops!” ); throw exceptionObject;

  32. Exercises • 9. Is the following legal? Exception exceptionObject = new Exception( “Oops!” ); throw exceptionObject; class test { public static void main ( String[] s ) { Exception exceptionObject = new Exception( "Oops!" ); throw exceptionObject; } } We need to do one more thing to get it to compile! javac test.java test.java:6: unreported exception java.lang.Exception; must be caught or declared to be thrown throw exceptionObject; ^ 1 error

  33. Exercises • 9. Is the following legal? Exception exceptionObject = new Exception( “Oops!” ); throw exceptionObject; class test { public static void main ( String[] s ) throws Exception { Exception exceptionObject = new Exception( "Oops!" ); throw exceptionObject; } } class test { public static void main ( String[] s ) throws Exception { Exception exceptionObject = new Exception( "Oops!" ); try { throw exceptionObject; } catch (Exception e) { } } }

  34. Try-catch blocks examples • Recall the Integer class (see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html). • What method do we use to convert a String into an int? • What happens if the String does not contain an int? • Try it. • What happens when we walk off of the end of an array? • Can you avoid this behavior?

  35. Useful exception subclasses • ArrayIndexOutOfBoundsException • NumberFormatException • IOException • NoSuchMethodException • FileNotFoundException

  36. Exercises 10. Define an exception class called PowerFailureException. • The class should have a ctor w/ no parameters. If an exception is thrown with this zero-argument ctor, getMessage should return “Power Failure!” • The class should also have a ctor w/ a single parameter of type String. If an exception is thrown w/ this ctor, then getMessage returns the value that was used as an argument to the ctor.

  37. x Exercises 10. Define an exception class called PowerFailureException. • The class should have a ctor w/ no parameters. If an exception is thrown with this zero-argument ctor, getMessage should return “Power Failure!” • The class should also have a ctor w/ a single parameter of type String. If an exception is thrown w/ this ctor, then getMessage returns the value that was used as an argument to the ctor.

  38. Exercises 11. Define an exception class called TooMuchStuffException. • The class should have a ctor w/ no parameters. If an exception is thrown with this zero-argument ctor, getMessage should return “Too much stuff!” • The class should also have a ctor w/ a single parameter of type String. If an exception is thrown w/ this ctor, then getMessage returns the value that was used as an argument to the ctor.

  39. Exercises 12. Suppose the exception class ExerciseException is defined as follows: public class ExerciseException extends Exception { public ExerciseException ( ) { super("Exercise Exception thrown!"); System.out.println("Exception thrown."); } public ExerciseException ( String message ) { super(message); System.out.println( "ExerciseException invoked with an argument."); } } What output would be produced by the following code (which is just an exercise and not likely to occur in a program)? ExerciseException e = new ExerciseException( “Do be do” ); System.out.println( e.getMessage() );

  40. Exercises 14. Suppose the exception class MyException is defined as follows: public class MyException extends Exception { public MyException ( ) { super("My Exception thrown!"); } public MyException ( String message ) { super("MyException: " + message); } } What output would be produced by the following code? int number; try { System.out.println( “try block entered” ); number = 42; if (number>0) throw new MyException( “Hi Mom!” ); System.out.println( “leaving try block” ); } catch (MyException exceptionObject) { System.out.println( exceptionObject.getMessage() ); } System.out.println( “end of example.” );

  41. Exercises 15. Suppose the exception class MyException is defined as follows: public class MyException extends Exception { public MyException ( ) { super("My Exception thrown!"); } public MyException ( String message ) { super("MyException: " + message); } } What output would be produced by the following code? int number; try { System.out.println( “try block entered” ); number = 42; if (number>0) throw new MyException( “Hi Mom!” ); System.out.println( “leaving try block” ); } catch (Exception exceptionObject) { //was MyException System.out.println( exceptionObject.getMessage() ); } System.out.println( “end of example.” );

  42. Exercises 16. Suppose the exception class MyException is defined as follows: public class MyException extends Exception { public MyException ( ) { super("My Exception thrown!"); } public MyException ( String message ) { super("MyException: " + message); } } What output would be produced by the following code? int number; try { System.out.println( “try block entered” ); number = -58; //was 42 if (number>0) throw new MyException( “Hi Mom!” ); System.out.println( “leaving try block” ); } catch (MyException exceptionObject) { System.out.println( exceptionObject.getMessage() ); } System.out.println( “end of example.” );

  43. Multiple catch blocks • More general form of try-catch blocks: try { … } catch (NegativeNumberException e) { … } catch (DivisionByZeroException e) { … } The order of catch blocks is important as they are evaluated in sequence. So put most specific first.

  44. 19. What output will be produced by the following code? public class NegativeNumberException extends Exception { public NegativeNumberException ( ) { super( "Negative Number Exception!“ ); } public NegativeNumberException ( String message ) { super( message ); } } int n; try { n = 42; if (n > 0) throw new Exception(); else if (n < 0) throw new NegativeNumberException(); else System.out.println( “bingo!” ); } catch (NegativeNumberException e) { System.out.println( “first catch” ); } catch (Exception e) { System.out.println( “second catch” ); } System.out.println( “end of exercise” ); Exercises

  45. 20. What output will be produced by the following code? public class NegativeNumberException extends Exception { public NegativeNumberException ( ) { super( "Negative Number Exception!“ ); } public NegativeNumberException ( String message ) { super( message ); } } int n; try { n = -42; //was 42 if (n > 0) throw new Exception(); else if (n < 0) throw new NegativeNumberException(); else System.out.println( “bingo!” ); } catch (NegativeNumberException e) { System.out.println( “first catch” ); } catch (Exception e) { System.out.println( “second catch” ); } System.out.println( “end of exercise” ); Exercises

  46. 21. What output will be produced by the following code? public class NegativeNumberException extends Exception { public NegativeNumberException ( ) { super( "Negative Number Exception!“ ); } public NegativeNumberException ( String message ) { super( message ); } } int n; try { n = 0; //was 42 if (n > 0) throw new Exception(); else if (n < 0) throw new NegativeNumberException(); else System.out.println( “bingo!” ); } catch (NegativeNumberException e) { System.out.println( “first catch” ); } catch (Exception e) { System.out.println( “second catch” ); } System.out.println( “end of exercise” ); Exercises

  47. What output will be produced by the following code? public class NegativeNumberException extends Exception { public NegativeNumberException ( ) { super( "Negative Number Exception!“ ); } public NegativeNumberException ( String message ) { super( message ); } } int n; try { n = -42; //was 42 if (n > 0) throw new Exception(); else if (n < 0) throw new NegativeNumberException(); else System.out.println( “bingo!” ); } catch (Exception e) { System.out.println( “first catch” ); } catch (NegativeNumberException e) { System.out.println( “second catch” ); } System.out.println( “end of exercise” ); Exercises

  48. Methods can throw exceptions, and/or call methods that throw exceptions. Catch or declare rule: Such a method must then contain a try-catch block (already discussed), and/or the function heading must specify that the method may throw an exception. The catch or declare rule is not always enforced. Checked exceptions (descendents of Exception) Unchecked exceptions (descendents of RuntimeException) Throwing exceptions in methods

  49. Declare (a method that indicates) that an exception may be thrown • Ex. public int f ( int x ) throws Exception { … } public boolean g ( ) throws DivideByZeroException, SomeOtherException { … }

  50. Useful methods that may throw exceptions • What method can be used to convert strings to integers? • Open a file for reading (see http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileInputStream.html) • The Scanner class (see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#nextInt())

More Related