Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.

Similar presentations


Presentation on theme: "SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions."— Presentation transcript:

1 SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions

2 SE-1020 Dr. Mark L. Hornick 2 A few of 60+ exception classes

3 SE-1020 Dr. Mark L. Hornick 3 Catch or let go? The Error class represents serious problems that should not be caught by ordinary applications (e.g. thread death, out-of-memory). The Exception class represents error conditions that should be caught (e.g. divide-by-0, null reference, conversion errors).

4 SE-1020 Dr. Mark L. Hornick 4 Catching different types of Exceptions When there are multiple catch blocks in a try-catch statement, they are checked in sequence. It is important to check more specialized exception classes before the more general exception classes. When an exception is thrown, its matching catch block is executed and the other catch blocks are ignored.

5 SE-1020 Dr. Mark L. Hornick 5 Fumbling to the VM If none of the catch blocks matches the thrown exception, the system will search down the stack trace for a method with a matching catch block. If none is found, the system (VM) will handle the thrown exception And terminate your program after calling printStackTrace()

6 SE-1020 Dr. Mark L. Hornick 6 Getting information from exceptions There are two methods of the Throwable class that provide information about the thrown exception: getMessage – a String description printStackTrace – call stack dump

7 SE-1020 Dr. Mark L. Hornick 7 Catching Exceptions If there is a block of code that must be executed regardless of whether an exception is thrown, we use the reserved word finally: inputStr = JOptionPane.showInputDialog(null, “”); try{ number = Integer.parseInt(inputStr); return;// will not return until finally is processed } catch (NumberFormatException e) { System.out.println(“Cannot convert to int”); return; ;// will not return until finally is processed } finally { System.out.println(“DONE”); }

8 SE-1020 Dr. Mark L. Hornick 8 You can also throw exceptions An exception is thrown using the throw statement. throw where is an instance of the Throwable class or its subclasses. Exception is a subclass of Throwable By convention, we create Exceptions rather than Throwable objects

9 SE-1020 Dr. Mark L. Hornick 9 A few of 60+ exception classes Throwable is the root of all Exceptions and Errors Your program cannot catch or throw Errors Your program can catch or throw Exceptions

10 SE-1020 Dr. Mark L. Hornick 10 Throwing your own Exceptions inputStr = JOptionPane.showInputDialog(null, “Enter a value less than 100”); number = Integer.parseInt(inputStr); if (num>=100) { throw new Exception(“Out of bounds”); }

11 SE-1020 Dr. Mark L. Hornick 11 Passing the buck When a method may throw an exception, either directly or indirectly, the method is called an exception thrower. An exception thrower method can be a: Catcher – if it catches it Propagator – if it lets it go Can be both if it catches some types and lets others go

12 SE-1020 Dr. Mark L. Hornick 12 Catcher/Propagator Example inputStr = JOptionPane.showInputDialog(null, “Enter a value less than 100”); try{ number = Integer.parseInt(inputStr); if (num>=100) { throw new Exception(“Out of bounds”); } } catch (NumberFormatException e) { System.out.println(“Cannot convert to int”); } Here, NumberFormatExceptions are caught but Exceptions are propagated. The “Out of bounds” exception will not be caught here!

13 SE-1020 Dr. Mark L. Hornick 13 RuntimeException – a special class of Exception Some exceptions have to be caught by your code and not allowed to propagate to the VM A checked exception is an exception that is checked at compile time RuntimeException and classes that derive from it are unchecked. They are detected only at runtime

14 SE-1020 Dr. Mark L. Hornick 14 Only RuntimeExceptions can be left unchecked RuntimeExceptions and descendants are unchecked exceptions Exception and IOException are checked exceptions

15 SE-1020 Dr. Mark L. Hornick 15 Propagating Exceptions If a method is a checked exception propagator, its header must be modified to declare the type of exceptions the method propagates. Without the required throws clause, the program will not compile. The reserved word throws is used for this declaration. void SomeMethod( ) throws Exception { // some code that may cause an Exception // that is not caught here in the method... }

16 SE-1020 Dr. Mark L. Hornick 16 Callers of a method that can throw a checked exception must include the try-catch statement in the method body or the throws clause in the header.

17 SE-1020 Dr. Mark L. Hornick 17 Throws is optional for RuntimeExceptions For the exceptions of the type (or derived from the type) called RuntimeException, the throws clause is optional. If a method is a propagator of runtime exceptions or errors, the throws clause is optional.

18 SE-1020 Dr. Mark L. Hornick 18 It is optional for callers of a method that can throw runtime exceptions to include the try-catch statement in the method body or the throws clause in the header.


Download ppt "SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions."

Similar presentations


Ads by Google