EXCEPTIONS Def An exception is a runtime error

  • Slides: 7
Download presentation
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by

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

Handling Exceptions within Applications The try-catch statement This includes a block of statements that

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.

n When an exception occurs, the catch block handling the corresponding exception type is

n 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. n The try-catch statement may be augmented by a “finally” block. n The finally block is executed whether the try statements execute successfully or a catch clause is executed. n See examples bottom p. 536 (Lew&Loftus)

n If an exception is not caught and handled where it occurs, control is

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

The “throws” clause in a method header. n Exceptions are classified as either “checked”

The “throws” clause in a method header. n Exceptions are classified as either “checked” or “unchecked”. n 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. n Example: IOExceptions (see IOSample. java) n An unchecked exception requirres no throws clause. These are only of Runtime. Exception or its descendants.

I/O Concepts n Def: Stream – an ordered sequence of bytes that flows from

I/O Concepts n Def: Stream – an ordered sequence of bytes that flows from a source to a destination. n A stream is either an input stream (for reading information) or an output stream (for writing information). n 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.

The class System n The class System contains three public static object n n

The class System n The class System contains three public static object n n 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 throws IOException in the method headers.