Presentation is loading. Please wait.

Presentation is loading. Please wait.

EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.

Similar presentations


Presentation on theme: "EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data."— Presentation transcript:

1 EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.

2 Handling Exceptions within Applications The try-catch statement This includes a block of statements that may “throw an exception” – the “try” block. That is, where an exception may occur. This also includes 1 or more “catch” clauses. Each catch clause is a handler for a different type exception. If no exceptions are thrown, what happens? The catch clauses are skipped.

3 When an exception occurs, the catch block handling the corresponding exception type is executed, and then control is transferred to the statement immediately following the try-catch statement. The try-catch statement may be augmented by a “finally” block. The finally block is executed whether the try statements execute successfully or a catch clause is executed. See examples bottom p. 536 (Lew&Loftus)

4 If an exception is not caught and handled where it occurs, control is returned to each preceding method that was called. Refer to pp.539-541. What lines of code are never executed because the exception was not caught? We can write our own exception classes by having them extend the class Exception, or one of its descendants. Hierarchy of Java exception classes – p.542

5 The “throws” clause in a method header. Exceptions are classified as either “checked” or “unchecked”. Checked exceptions must be either caught or must be listed in the throws clause of every method that may either catch it or propagate it. Example: IOExceptions (see IOSample.java) An unchecked exception requirres no throws clause. These are only of RuntimeException or its descendants.

6 I/O Concepts Def: Stream – an ordered sequence of bytes that flows from a source to a destination. A stream is either an input stream (for reading information) or an output stream (for writing information). A program can handle multiple streams of both types but a particular store of data, or file, can serve as only an input stream or an output stream, but not both at the same time.

7 The class System The class System contains three public static object variables for I/O: System.in, System.out, System.err. These objects represent the standard IO devices: keyboard for input and monitor for output. Many other classes exist in the Java standard class library for defining I/O streams for dealing with streams of data for files and for data that is not treated as raw bytes but as characters. Most I/O class operations require the throws IOException in the method headers.


Download ppt "EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data."

Similar presentations


Ads by Google