Hi,
This section is about exception handling in Java. I will try to cover as much use cases as I can. The best way to learn exceptions is to play with different exception types, especially handling multiple exceptions, by yourself.
We are responsible for following specific classes for Java 8 Programmer I exam: Error, Unchecked Exception, and Checked Exception.
Error
They should not be handled by a programmer.
ExceptionInInitializerError StackOverflowError NoClassDefFoundError
Unchecked Exception (Runtime Exception)
They are called as “Runtime Exceptions” as well. It is optional to handle them.
ArithmeticException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException NullPointerException NumberFormatException
Checked Exception
Exception and all subclasses that do not extend RuntimeException. They should be either declared or handled.
FileNotFoundException IOException
Declare: “Throws” Declares An Exception That Might Be Throwed.
static void foo() throws Exception {
}
Handle: “Throw” Means You Want To Throw Exception.
static void foo() throws Exception {
throw new Exception();
}
If you throw exception without declaring, you will get a compiler error “Unhandled exception type Exception”. In this case, we can handle exception in either caller or called function with try/catch.
static void foo() {
throw new Exception(); // Compile Error
}
(Solution 1) Handling an exception in called function (caller).
/**
* Throwing an exception in called function surround called function with try/catch
*
* @author suleyman.yildirim
*
*/
public class UnhandledExceptionSolution1 {
public static void main(String[] args) {
foo();
}
static void foo() {
try {
throw new Exception();
} catch (Exception e) {
// TODO: handle exception
}
}
}
(Solution 2) Handling an exception in caller function (callee).
/**
* Throwing an exception in caller function
*
* Surround the caller function with try/catch
* Declare and throw exception without using try/catch in called function
*
* @author suleyman.yildirim
*
*/
public class UnhandledExceptionSolution2 {
public static void main(String[] args) {
try {
foo();
} catch (Exception e) {
// TODO: handle exception
}
}
static void foo() {
throw new Exception();
}
}
A short remark on the previous example is that you can avoid compile error by declaring exception in main function. However, you will get runtime error as you do not actually “handle” the exception you throwed. Try following code and see what happens 
/**
* Runtime Error!
*
* Once you throw exception in a function, you should either handle it in caller
* or called function. Otherwise, you get runtime exception.
*
* @author suleyman.yildirim
*
*/
public class RuntimeError {
public static void main(String[] args) throws Exception {
foo();
}
static void foo() throws Exception {
throw new Exception();
}
}
Handling Multiple Exception Types
If there is an IS-A relationship between exception classes, the order is important. The derived most class should be caught first. Otherwise, compiler warns us like “Unreachable catch block for ArithmeticException. It is already handled by the catch block for RuntimeException”. Consider following example.
try {
...
} catch (RuntimeException e) {
throw e;
} catch (ArithmeticException e) { // Compile error
throw e;
} catch (Exception e) {
throw e;
}
If there is not an IS-A relationship, the order is not important. Consider two classes below, ExceptionA and ExceptionB, which are inherited from different type of exceptions.
class ExceptionA extends RuntimeException {
}
class ExceptionB extends ArithmeticException {
}
void f() {
try {
...
} catch (ExceptionA e) {
throw e;
} catch (ExceptionB e) {
throw e;
}
}
Nenhum comentário:
Postar um comentário