Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Exceptions a quick review….

Similar presentations


Presentation on theme: "Java Exceptions a quick review…."— Presentation transcript:

1 Java Exceptions a quick review…

2 What happened before exceptions?
must remember to check return value OR, must pass label/exception handler to every function Function Caller return status label to subprogram Function Caller Fortran strategy

3 Terminology Review An exception is an unusual event detectable by either hardware or software An exception is raised/thrown when its associated event occurs The exception handler code determines what to do

4 Advantages of exceptions
Error detection code is tedious to write and it clutters the program Exception propagation allows a high level of reuse of exception handling code Encourage programmers to consider all events that could occur that might need to be handled – especially checked exceptions.

5 Exception Propagation
main main calls function a a calls function b exception occurs in b If no try/catch in b, exception propagates to a If no try/catch in a, exception propagates to main If no try/catch in main, execution terminates (in C++, can override default handler, named unexpected) Continuation. If exception is handled, execution continues after last catch (unless handler calls exit of course). call a call b Problem! Both Java and C++ propagate exceptions

6 Java Exception Handling
The Java library includes two subclasses of Throwable: Error Thrown by the Java interpreter for events such as heap overflow Never handled by user programs Exception User-defined exceptions are usually subclasses of this Has two predefined subclasses, IOException and RuntimeException (e.g., ArrayIndexOutOfBoundsException and NullPointerException)

7 Checked and Unchecked (review)
Exceptions of class Error and RunTimeException and all of their descendants are called unchecked exceptions All other exceptions are called checked exceptions (compiler checks that your code handles – all exceptions occur at runtime) Methods containing code that can throw a checked exception must either: List the exception(s) in a throws clause, or Handle the exception (e.g., with try/catch) Example throws clause (not the same as throwing an exception!): public void myFunction() throws Exception

8 Checked miconceptions
“Checked” means that Java checks that you know a line of code can generate an error The compiler does NOT know that an exception will occur… it knows that an exception might occur! ALL exceptions occur at RUNTIME

9 Java Details A method that calls a method with a checked exception in its throws clause has three alternatives: Catch and handle the exception Catch the exception and throw an exception that is listed in its own throws clause Declare that exception in its throws clause and do not handle it public void A() throws BadException { … } public void B() { … try { A() } catch (BadException e) { … } } public void C() throws AnException{ throw (AnException) } public void D() throws BadException { … A() … }

10 Imagine if NullPointer was checked
Point p; Point p2 = new Point(3,4); try { int x = p.getX(); } catch (NullPointerException e) { syso(“there’s a logic but here”); } // compiler doesn’t execute… so even though // we know p2 is OK, the compiler wouldn’t int x = p2.getX();

11 Overriding methods A method cannot declare more exceptions in its throws clause than the method it overrides. Example: If you override run in a Thread class, you cannot add a throws clause.

12 Java Finally Can appear at the end of a try construct Form:
... } Purpose: To specify code that is to be executed, regardless of what happens in the try construct (e.g., to close file or database connection). Finally is executed even if there is a return statement in the block.

13 Finally example try { for (index = 0; index < 100; index++) { …
if (…) { return; } //** end of if } //** end of for loop } //** end of try clause finally { } //** end of try construct * Java exercise has examples to play with


Download ppt "Java Exceptions a quick review…."

Similar presentations


Ads by Google