Java - Exception Handling
THROW vs THROWS
*throw:* 1. Java throw keyword is used to explicitly throw an exception. 2. Checked exception cannot be propagated using throw only. 3. Throw is followed by an instance. 4. Throw is used within the method. 5. You cannot throw multiple exceptions. *throws:* 1. Java throws keyword is used to declare an exception. 2. Checked exception can be propagated with throws. 3. Throws is followed by class. 4. Throws is used with the method signature. 5. You can declare multiple exceptions e.g. public void method()throws IOException,SQLException.
Try/Catch - Handler
- A catch block is called a handler because it handles an exception
Throw statement
- appears within a try block; if reached, execution jumps immediately to the end of the try block. - code is written so only error situations lead to reaching a throw - provides an object of type Throwable, such as an object of type Exception or its subclasses. - The object thrown and caught must be of the Throwable class type, or a class inheriting from Throwable.
Try/Catch - Catch clause
- immediately follows a try block. - The clause is said to catch the thrown exception. - A catch block is called a handler because it handles an exception. - if the catch was reached due to an exception thrown of the catch clause's parameter type, the clause executes.
Exception-handling constructs
1. try 2. throw 3. catch
zz- True/False
A checked exception must either be handled via try-catch constructs, or the throwing method must specify that the appropriate exception type may be thrown by appending a throws clause to the method's definition. (True)
Input/Output
A file input/output stream requires exception handling to ensure invalid or interrupted file operation exceptions are appropriately caught and handled.
zz- True/False
A goal of exception handling is to avoid polluting normal code with distracting error-handling code. (True)
zz- True/False
A second catch block can never execute immediately after a first one executes. (True)
zz- True/False
A throw executed in a method automatically causes a jump to the last return statement in the method. (False)
zz- True/False
After an exception is thrown and a catch block executes, execution resumes after the throw statement (False)
z - What type of exception may be thrown by a FileReader's constructor?
File Not Found Exception
Exception - FileNotFoundException
FileReader's constructors may throw a FileNotFoundException, which itself inherits from IOException, if the specified file cannot be opened for reading.
zz- True/False
For a method that may contain a throw, all of the method's statements, including the throw, must be surrounded by a try block. (False)
z - What is the name of the base class from which all file i/o exceptions discussed above are derived?
IOException
z - What type of exception may be thrown by a FileReader's read() and close() methods?
IOException
z - What type of exception may be thrown by a FileWriter's constructor?
IOException
Throws clause
If a method throws an exception not handled within the method, a programmer must include a throws clause within the method declaration, by appending throws Exception before the opening curly brace.
zz- True/False
If no throw is executed in a try block, then the subsequent catch block is not executed. (True)
Try/Catch - catch(Throwable thrwObj)
a catch-all handler that catches any error or exception as both are derived from the Throwable class; - this handler is useful when listed as the last handler. - A thrown exception may also be caught by a catch block meant to handle an exception of a base class.
Exception
a circumstance that a program was not designed to handle. A problem that arises during the execution of a program. Occurs when the normal flow of a program is disrupted. - Checked Exceptions - Unchecked Exceptions - IOException (fileReader class; and subclass of Exception) - FileNotFoundException - ArrayindexOutofBoundsException - RuntimeException (subclass of Exception class)
Try/Catch - Finally block
a code block that is used to execute the important code such as closing connection, stream, etc. is ALWAYS executed whether exception is handled or not. follows all catch blocks. executes after the program exits the corresponding try or catch blocks. Note: - the finally block will NOT be executed if program exits (either by calling System.exit(), or by causing a fatal error that causes the process to abort).
Exceptions: Checked exception
an exception that a programmer should be able to anticipate and appropriately handle. - Checked exceptions include Exception and several of its subclasses.
Exceptions: Unchecked exception
exceptions that result from hardware or logic errors that typically cannot be anticipated or handled appropriately. - should be eliminated from the program or at the very least should cause the program to terminate immediately. - A programmer is not required to handle unchecked exceptions or even specify that a method may throw them. Unchecked exceptions are comprised of the Error and RuntimeException classes and their subclasses. - Examples of built-in unchecked exceptions: NullPointerException, ArithmeticException, IndexOutOfBoundsException, and IOError.
z - What is the name of the clause used to ensure that contained statements are executed after the try and catch blocks.
finally
Exception - getMessage method
in a catch blow, we can access the message that is contained in an exception that is thrown. The message is set when the exception is created and thrown. public class ThrowableDemo { public static void main(String[] args) throws Throwable { try { newException(); } catch(Throwable e) { System.err.println(e); /* returns the detail message string of this Throwable instance */ System.out.println(e.getMessage()); } } public static void newException() throws Exception { System.out.println("This is newException() function"); throw new Exception("new Exception..."); } }
Exception - Exception handling
is behavior of a component or system in response to erroneous input. - exception handling provides a flexible mechanism for passing control from the point of error reporting to a competent recovery handler.
FileReader class
provides an input stream that allows a programmer to read characters from the file specified via FileReader's constructor. - Most FileReader methods and constructors throw exceptions of type IOException, a built-in checked exception inheriting from the Exception class.
FileWriter
provides overloaded write() methods to write a stream of characters, and a close() method to flush the underlying buffer and close the stream. - Both of these methods may throw IOExceptions in the event of a failure. - FileWriter's constructor may throw an IOException if the program or operating system cannot create (or open) the specified file.
Try/Catch - Try block
surrounds normal code, which is exited immediately if a throw statement executes.
Try/Catch - Catch block parameter
when a catch block is created, it must specify what type of exception it will catch, and gives that exception a name that will be used inside of the catch block when referring to the exception that was caught.