Download presentation
Presentation is loading. Please wait.
1
Handling Errors with Exception (in Java) Project 10 CSC 420
2
What’s an exception? “Exception” means exceptional event that occurs during the execution of a program that disrupts the normal flow of instructions Many kinds of errors cause exception; hardware errors, hard disk crash, or simple programming error When such errors occurs in Java, the method creates an exception object then hands it off to the runtime system; is called throwing an exception in Java The runtime system then finds a way to handle the exception or finds a method that contains an appropriate exception handler
3
Cont’d.. If the runtime system fails to find an appropriate exception handler, then the program and the runtime system terminates Java has 3 advantages by using exceptions to manage errors -separating error handling code from regular code -propagating errors in the call stack -grouping error types and error differentiation
4
How do Java deal with it? Catch method catches an exception by providing an exception handler Specify is a method is the ability to throw an exception if chose not to catch that exception Checked exceptions are not runtime exceptions and are checked by the compiler
5
How to write exception handlers? Try block is the first step In building an exception where you enclose that might throw an exception { System.out.println(“entering try statement"); out = new PrintWriter( new FileWriter(“outFile.txt")); for (int i = 0; i < size; i++) out.println(“value at: " + i + " = " + victor.elementAt(i)); }
6
Cont’d.. Catch block is the association of exception handlers with a try block by giving one or more catch blocks after the try block {... } catch (... ) {... } catch (... )
7
Cont’d The finally block gives a device for cleaning up the state of the method before allowing control to a different part of the program finally { if (out != null) { System.out.println(“closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } }
8
Throw statements All java methods uses throw statements Requires a single argument, a throwable object throwable objects are instances of any subclass of the Throwable in Java systemThrowable throw someThrowableObject ; The compiler refuses to compile a program if you try to throw an object that is not throwable
9
Codes used in describing Exceptions Runtime –error that can occur in any code Parentclass java.lang.RuntimeException Error – serious errors such as out of memory Parentclass java.lang.RuntimeError Checked – can occur in some part of the code\ Parentclass java.lang. Exception
10
Web sources http://java.sun.com/docs/books/tutorial/essential/excepti ons/index.html http://www.javaworld.com/javaworld/jw-08-2000/jw-0818- exceptions.html http://mindprod.com/jglossexception.html http://citeseer.nj.nec.com/context/147839/0 http://www.eecs.umich.edu/gasm/papers/javaexc.html http://www.isr.uci.edu/~cjensen/info/ExceptionHandler.ht m
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.