1 / 10

Handling Errors with Exception (in Java)

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

yamka
Download Presentation

Handling Errors with Exception (in Java)

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. 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 system 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/exceptions/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.htm

More Related