1 / 19

Exceptions

Exceptions. cs1043. Program Exceptions. When a program detects an error, what should it do? Nothing, simply allow the program to fail. Implement a course of action to correct the error. Example:. A user enters an incorrect URL into a web browser. Should the browser crash?

elle
Télécharger la présentation

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. Exceptions cs1043

  2. Program Exceptions • When a program detects an error, what should it do? • Nothing, simply allow the program to fail. • Implement a course of action to correct the error.

  3. Example: • A user enters an incorrect URL into a web browser. • Should the browser crash? • Should the browser request another URL?

  4. Exception Handling • Software is designed to detect and correct most failures. • Detection and correction of an error is known as exception handling.

  5. Java Exceptions • The Java compiler places exceptions into two categories: • Checked exceptions • Unchecked exceptions

  6. Checked Exceptions • The compiler requires the programmer to either: • write a competent exception handler (CEH) for any checked exceptions. - Example: Input-output operations (reading and writing files and data streams). • Throw the exception hoping another method in the calling-tree will be able to handle the exception. This require the throws keyword.

  7. Unchecked Exceptions • Example double z = x / y; The compiler does not require any special action to prevent the division when y=0.

  8. Unchecked Exceptions • The programmer can choose to: • Ignore the exception and hope for the best. • Catch the exception and implement an action to recover from the exception. Note: if the program cannot recover from an exception, the program exits with a fatal exception runtime error.

  9. Unchecked Exception Example // Assume x and y are declared and // assigned. double z; if ( y != 0 ) //prevent 0-division z = x / y; else … // perform some other action

  10. Checked Exception • Two choices for the programmer: • Throw the exception to the referencing method and hope for recovery. • Catch the exception with a competent exception handler

  11. Java Keywords for Exception Handling • An unchecked exception could use the keyword “throw”. • Checked exceptions can use any of these: • throws • throw • try, catch, & finally

  12. The Competent Exception Handler • The competent exception handler uses a combination of these three keywords: try, catch, and finally. • If the programmer wishes to send the exception to the referencing method, then use the throws keyword.

  13. Syntax for CEH • In order: • try-block – one per CEH • catch-blocks - zero or more per CEH • finally-block – zero or one per CEH

  14. 1. CEH try-Block • A CEH must have a try-block • The try-block consists of the try keyword followed by a body of code.

  15. 2. CEH catch-Blocks • The try-block is followed by zero or more catch blocks. • Each catch block will perform an action based on the exception type

  16. 3. CEH finally-Block • The finally-block is required if there are zero catch blocks. • The finally-block is optional if there is at least one catch-block.

  17. CEH • The CEH can be nested just like other control structures.

  18. Unchecked Exception with “CEH”

  19. User Defined Exception Class

More Related