1 / 13

ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions

ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions. Overview. Problem: Can we detect run-time errors and take corrective action? Try-catch Test for a variety of different program situations Java provides a series of test cases

keefe-rocha
Download Presentation

ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions

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. ECE 122Engineering Problem Solving with JavaLecture 24Exceptions

  2. Overview • Problem: Can we detect run-time errors and take corrective action? • Try-catch • Test for a variety of different program situations • Java provides a series of test cases • Allows for graceful exit of program or treatment of special cases

  3. Errors • Errors do occur in programming. • Problems opening a file, dividing by zero, accessing an out-of-bounds array element, hardware errors, and many more • The question becomes: What do we do when an error occurs? • How is the error handled? • Who handles it? • Should the program terminate? • Can the program recover from the error? Should it? • Java uses exceptions to provide the error-handling capabilities for its programs.

  4. Exceptions • An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. • Represented as an object in Java • Throwing an exception • An error occurs within a method. An exception object is created and handed off to the runtime system. The runtime system must find the code to handle the error. • Catching an exception • The system searches for code to handle the thrown exception. It can be in the same method or in some method in the call stack.

  5. Exceptions • An exception is an object that describes an unusual or erroneous situation • Exceptions are thrown by a program • May be caught and handled by another part of the program • A program can be separated into a normal execution flow and an exception execution flow • An error is also represented as an object in Java • Usually represents a unrecoverable situation and should not be caught

  6. Handling Exceptions • General form: try { statement(s); } catch (ExceptionTypename) { statement(s); } finally { statement(s); }

  7. Exception Handling • Java has a predefined set of exceptions and errors that can occur during execution • A program can deal with an exception in one of three ways: • ignore it • handle it where it occurs • handle it an another place in the program • The manner in which an exception is processed is an important design consideration

  8. Exception Handling • If exception is ignored by the program • The program will terminate abnormally and produce an appropriate message • The message includes a call stack trace that: • indicates the line on which the exception occurred • shows the method call trail that lead to the attempted execution of the offending line

  9. Exception (derived from Throwable) RunTimeException ClassNotFoundException IOException ArithmeticException This is just a small part of the Exception hierarchy NullPointerException IndexOutOfBoundsException NumberFormatException Exception Class Hierarchy • Java has a predefined set of exceptions and errors that can occur during execution.

  10. The try Statement • Handling an exception in a program • The line that throws the exception is executed within a try block • A try block is followed by one or more catch clauses • Each catch clause has an associated exception type and is called an exception handler • When exception occurs: • Processing continues at the first catch clause that matches the exception type

  11. Handling Exceptions • Three statements help define how exceptions are handled: • try- identifies a block of statements within which an exception might be thrown • catch - must be associated with a try statement and identifies a block of statements that can handle a particular type of exception. • The statements are executed if an exception of a particular type occurs within the try block. • A try statement can have multiple catch statements associated with it. • finally - must be associated with a try statement • identifies a block of statements that are executed regardless of whether or not an error occurs within the try block. • Even if the try and catch block have a return statement in them, finally will still run.

  12. The finally Clause • A try statement can have an optional clause following the catch clauses • Designated by the reserved word finally • The statements in the finally clause always are executed • If no exception is generated: • The statements in the finally clause are executed after the statements in the try block complete • If an exception is generated: • The statements in the finally clause are executed after the statements in the appropriate catch clause complete

  13. Summary • Exceptions form an important part of Java • Graceful error checking and handling allow for better designed code • Try-catch evaluates a number of possibilities • Often error messages are the result

More Related