* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Handling Errors with Exception (in Java)
Survey
Document related concepts
Library (computing) wikipedia , lookup
Program optimization wikipedia , lookup
Go (programming language) wikipedia , lookup
Resource management (computing) wikipedia , lookup
Object-oriented programming wikipedia , lookup
Scala (programming language) wikipedia , lookup
Java syntax wikipedia , lookup
Coding theory wikipedia , lookup
Control flow wikipedia , lookup
Name mangling wikipedia , lookup
Java (programming language) wikipedia , lookup
Structured programming wikipedia , lookup
C Sharp syntax wikipedia , lookup
Transcript
Handling Errors with Exception (in Java) Project 10 CSC 420 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 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 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 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)); } 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 ( . . . ) 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"); } } 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 system throw someThrowableObject ; The compiler refuses to compile a program if you try to throw an object that is not throwable 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 Web sources http://java.sun.com/docs/books/tutorial/essential/excepti ons/index.html http://www.javaworld.com/javaworld/jw-08-2000/jw-0818exceptions.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.htm