1 / 17

Programming with Java

Programming with Java . COP 2800 Lake Sumter State College Mark Wilson, Instructor. Programming with Java. Exceptions. Code Risks. Methods with unknown side effects Versioning Latent bug Methods that may not work at runtime Unavailable system resources Abnormal or unexpected response.

kanan
Download Presentation

Programming with 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. Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor

  2. Programming with Java Exceptions

  3. Code Risks • Methods with unknown side effects • Versioning • Latent bug • Methods that may not work at runtime • Unavailable system resources • Abnormal or unexpected response

  4. Exception An event that occurs during the execution of a program that disrupts the normal flow of instructions Method Java uses to handle errors and other exceptional events An exception object, contains information about the error, including its type and the state of the program when the error occurred Creating an exception object and handing it to the runtime system is called throwing an exception.

  5. Handling Exceptions Traditional processing breaks well formed constructs, makes spaghetti code The runtime system searches the call stack for a method that contains a block of code that can handle the exception (exception handler) An exception handler is considered appropriate if the type of the exception object thrown matches the type that can be handled by the handler Uncaught exceptions cause the runtime system (and, therefore, the program) to terminate

  6. Advantages • Separates Error-Handling Code from "Regular" Code • In traditional programming, error detection, reporting, and handling often lead to confusing spaghetti code • Propagating Errors Up the Call Stack • Java runtime environment searches backward through the call stack to find any methods that are interested in handling a particular exception • Grouping and Differentiating Error Types • grouping or categorizing of exceptions is a natural outcome of the class hierarchy

  7. Call Stack

  8. Exception Categories • Checked Exception – Exception class • well-written application anticipates and recovers • Error – Error class • external to the application • application usually cannot anticipate or recover • Runtime Exception – RuntimeExceptionclass • internal to the application • application usually cannot anticipate or recover • Error and runtime exceptions are unchecked exceptions

  9. Syntax • throws • Method declares it may throw an exception • try • Sets off the code block for risky behavior • catch • Sets off code to handle an exception • Follows try • Multiple catches possible • finally • Always executes after the try block • May not execute if the JVM exits during a tryor catch • Good place to put cleanup code (e.g. close open files)

  10. Syntax Example public void writeList() { PrintWriterout = null; try { System.out.println("Entering" + " try statement"); out = new PrintWriter(new FileWriter("OutFile.txt")); for (inti = 0; i < SIZE; i++) out.println("Value at: " + i + " = " + vector.elementAt(i)); } catch (ArrayIndexOutOfBoundsExceptione) { System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } } }

  11. Throwing Exceptions public void writeList() throws IOException, ArrayIndexOutOfBoundsException { public void writeList() throws IOException { Methods that don’t catch checked exceptions must specify that they can throw exceptions Identifies exceptions that may need to be caught Unchecked exceptions are optional

  12. Throwing Exceptions public Object pop() { Object obj; if (size == 0) { throw new EmptyStackException(); } obj= objectAt(size - 1); setObjectAt(size - 1, null); size--; return obj; } Any code can throw exceptions Exceptions are thrown with throw All exceptions are descendants of Throwable Most programs throw and catch objects derived from the Exception class

  13. ThrowableHeirarchy

  14. Creating Exception Classes Exception type isn't represented by those in the Java platform? Help users differentiate your exceptions from other vendors? Does your code throw more than one related exception? Will users have access to other’s exceptions? Most applications will throw objects that are Exceptions. Errors are normally used for serious, hard errors in the system For readable code, it's good practice to append the string Exception to the names of all classes that inherit (directly or indirectly) from the Exception class.

  15. Exception Summary A program can use exceptions to indicate that an error occurred. To throw an exception, use the throw statement and provide it with an exception object — a descendant of Throwable — to provide information about the specific error that occurred. A method that throws an uncaught, checked exception must include a throws clause in its declaration. A program can catch exceptions by using a combination of the try, catch, and finally blocks. The try block identifies a block of code in which an exception can occur. The catch block identifies a block of code, known as an exception handler The finally block identifies a block of code that is guaranteed to execute, The try statement should contain at least one catch block or a finally block and may have multiple catch blocks. The class of the exception object indicates the type of exception thrown. The exception object can contain further information about the error With exception chaining, an exception can point to the exception that caused it

  16. Programming with Java Packages, Jars and Deployment. Oh, my!

  17. Programming with Java Beginning GUI

More Related