In Java, there are two types of exceptions:
1. Checked Exception
2. Unchecked Exception
Example 1:
The java.io.IOException is a checked exception. The method1() throws this exception.
Example 2:
If some code throws a checked exception within a method, then it must be caught within try/catch or throws keyword.
The following 2 implementations of method2() will compile.
Using try/catch block.
The following will not compile because checked exception not handled in either try/catch block or throws keyword.
Using try/catch block.
if (!isResourceAvailable) {
throw new IOException();
}
} catch(IOException e) {
}
}
Declaring throws keyword
Example 4:
1. Checked Exception
2. Unchecked Exception
1. Checked Exception
These exceptions are check at compile time. If a method throws a checked exception, then the caller have to either caught by throws or try/catch block.Example 1:
The java.io.IOException is a checked exception. The method1() throws this exception.
void method1() throws IOException
{
}
The following method2() gives compile error because it calls method1() but doesn't throws IOException or doesn't caught it within try/catch block.
void method2()
{
method1();
}
{
method1();
}
Error when compile:
unreported exception IOException; must be caught or declared to be thrown method1();Example 2:
If some code throws a checked exception within a method, then it must be caught within try/catch or throws keyword.
The following 2 implementations of method2() will compile.
Using try/catch block.
void method2()
{
try
{
method1();
}
catch(IOException e)
{
}
}
{
try
{
method1();
}
catch(IOException e)
{
}
}
Declaring throws keyword.
void method2() throws IOException
{
method1();
}
{
method1();
}
Example 3:The following will not compile because checked exception not handled in either try/catch block or throws keyword.
public void ioOperation(boolean isResourceAvailable)
{
if (!isResourceAvailable) {
throw new IOException();
}
}
if (!isResourceAvailable) {
throw new IOException();
}
}
The following 2 implementations of ioOperation() will compile.Using try/catch block.
public void ioOperation(boolean isResourceAvailable)
{
try {
try {
if (!isResourceAvailable) {
throw new IOException();
}
} catch(IOException e) {
}
}
Declaring throws keyword
public void ioOperation(boolean isResourceAvailable) throws IOException
{
if (!isResourceAvailable) {
throw new IOException();
}
}
{
if (!isResourceAvailable) {
throw new IOException();
}
}
2. Unchecked Exception
These exceptions are not checked at compile time. In java, Under Error and RunimeException classes are unchecked exceptions, everything else under throwable classes are checked exceptions.
+-----------+
| Throwable|
+----------- +
/ \
/ \
+-------+ +----------- +
| Error | | Exception |
+-------+ +----------- +
/ | \ / | \
\______/ \_____/ \
unchecked checked \
+---------------------- +
| Throwable|
+----------- +
/ \
/ \
+-------+ +----------- +
| Error | | Exception |
+-------+ +----------- +
/ | \ / | \
\______/ \_____/ \
unchecked checked \
+---------------------- +
| RuntimeException |
+---------------------- +
+---------------------- +
/ | | \
\_________________/
\_________________/
unchecked
Example 4:
Consider the following Java program. It compiles fine, but it throws ArithmeticException when run. The compiler allows it to compile, because ArithmeticException is an unchecked exception.
class Main {
public static void main(String args[]) {
int x = 0;
int y = 10;
int z = y/x;
}
}
public static void main(String args[]) {
int x = 0;
int y = 10;
int z = y/x;
}
}
Example 5:
The method3() throw RuntimeException. But doesn't require to caught within try/catch or throws keyword. It'll compile fine.
void method3()
{
throw new RuntimeException();
}
{
throw new RuntimeException();
}
As for the particular questions:
- Is the
NumberFormatException
consider a checked exception?
No.NumberFormatException
is unchecked (= is subclass ofRuntimeException
). Why? I don't know. (but there should have been a methodisValidInteger(..)
) - Is
RuntimeException
an unchecked exception?
Yes, exactly. - What should I do here?
It depends on where this code is and what you want to happen. If it is in the UI layer - catch it and show a warning; if it's in the service layer - don't catch it at all - let it bubble. Just don't swallow the exception. If an exception occurs in most of the cases you should choose one of these:
- log it and return
- rethrow it (declare it to be thrown by the method)
- construct a new exception by passing the current one in constructor
- Now, couldn't the above code also be a checked exception? I can try to recover the situation like this? Can I?
It could've been. But nothing stops you from catching the unchecked exception as well - Why do people add class
Exception
in the throws clause?
Most often because people are lazy to consider what to catch and what to rethrow. ThrowingException
is a bad practice and should be avoided.
No comments:
Post a Comment