Exception handling constructs such as the throw statement, throws clause, and catch clause deal only with Throwable and its subclasses. There are three important subclasses of Throwable that you need to learn in detail: the Error, Exception, and RuntimeException
The Exception Class
Exceptions of type Exception are known as checked exceptions. If code can throw an Exception, you must handle it using a catch block or declare that the method throws that exception, forcing the caller of that method to handle that exception.
CheckedExceptionExample2.java
package com.appfworks.exception;
/**
* Created by suresh on 02/11/15.
*/
import java.io.*;
class CheckedExceptionExample2 {
public static void main(String []args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream(args[0]);
}
}
Important Subclasses of the Exception Class
The RuntimeException Class
RuntimeException is a derived class of the Exception class. The exceptions deriving from this class are known as unchecked exceptions
Runtime exceptions such as ArrayIndexOutOfBoundsException indicate likely programming errors, and you should fix the code instead of catching and handling the exceptions. So, how do you fix the program here? You can check the length of args before attempting to access the array member.
UnCheckedExceptionExample3.java
package com.appfworks.exception;
/**
* Created by suresh on 02/11/15.
*/
import java.io.*;
class UnCheckedExceptionExample3 {
public static void main(String []args) throws FileNotFoundException {
// if any argument is passed, it would be greater than or equal to one
if(args.length >= 1) {
FileInputStream fis = new FileInputStream(args[0]);
} else {
System.out.println("Error: No arguments passed in the commandline!");
System.out.println("Pass the name of the file to open as commandline argument");
}
}
}
It is a good practice to perform defensive checks and avoid raising unnecessary runtime exceptions.
The Error Class
When the JVM detects a serious abnormal condition in the program, it raises an exception of type Error. When you get an exception of Error or its subtypes, the exception is not meant for you to handle. The best course of action is to let the program crash.
Exceptions of type Error indicate an abnormal condition in the program. There is no point in catching this exception and trying to continue execution and pretending nothing has happened. It is a really bad practice to do so!
The Throws Clause
A method can throw checked exceptions; the clause throws specifies these checked exceptions in the method signature.
If you look at the declaration of this Scanner method, you’ll see a throws clause:
public Scanner(File source) throws FileNotFoundException {So, any method that invokes this constructor should either handle this exception or add a throws clause to declare that the method can throw this exception.
An overriding method can declare more specific exceptions than the exception(s) listed in the throws clause of the base method; in other words, you can declare derived exceptions in the throws clause of the overriding method.




No comments:
Post a Comment