Reputation: 26324
What are the Runtime exceptions and what are Checked/Unchecked Exceptions and difference between Error/Exception.Why these many types? Instead Java may simply follow a simple design(just try/catch all types) to handle an abnormal condition in a program?
Upvotes: 26
Views: 23496
Reputation: 11153
Since I am a new Java developer, I have also faced some difficulties for distinguishing and dealing with different types of exceptions. That is why I have made a short note on this topic, and whenever I get confused I go through it. Here it is with the image of the Throwable
class hierarchy:
[image courtesy of JavaTpoint].
There are three key classes to remember here: Throwable
, Exception
and Error
. Among these classes Exception
can be divided into two types: "Checked Exception" and "Unchecked Exception".
Checked Exception:
Throwable
except RuntimeException
and Error
. try/catch
or indicate in the function signature that it throws
them and forcing us to deal with them in the caller.IOException
, SQLException
, etc.Unchecked Exception:
RuntimeException
are known as unchecked exceptions.ArithmeticException
,NullPointerException
, ArrayIndexOutOfBoundsException
, etc.ArithmeticException
, which can be avoided by a simple check on the divisor. Similarly we can avoid NullPointerException
by simply checking the references: if (object != null)
or even using better techniques.Error:
Error
refers to an irrecoverable situation that is not being handled by a try/catch
.OutOfMemoryError
, VirtualMachineError
, AssertionError
, etc.Why are these many types?
In addition to Stephen C's answer I want to say:
exception handling is a relatively expensive operation in Java. We should not put all exceptional situation in a try/catch
block. Excessive use of try/catch
s may hamper program performance.
In conclusion, Exception
s should be handled programmatically whenever possible. On the other hand, we cannot handle Error
s, so these might be some logical reasons why there are many types of exceptions.
Upvotes: 42
Reputation: 9
Exceptions are two types in java:
1. **Checked Exception: The exceptions which are checked by compiler.
For example: we you are performing operation with file, then compiler will ask you to handle IOException either by try-catch block or throws keyword.
2. Unchecked Exception: The exceptions which are not checked by compiler at run time.
For example: If you are performing operation on an object without creating it; in this case you'll get NullPointerException.
Upvotes: -2
Reputation: 193078
This article sumarizes Checked and Unchecked exceptions in a clear and concise way.
Checked Exceptions: Checked Exceptions are the exceptions which can be detected, identified and checked at compile time. If a code block throws a checked exception then the method must handle the exception or it must specify the exception using throws
keyword.
Example:
public void testDB() throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/selenium","root","root");
System.out.println("Connected to MySQL DB");
}
We either need to specify list of exceptions using throws or we need to use try-catch{}
block. I have demonstrated the useage of throws
in the below program.
Unchecked Exceptions: Unchecked Exceptions are not checked at compiled time. Java exceptions under Error
and RuntimeException
classes are unchecked exceptions and everything else under throwable is checked.
Summary: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
Upvotes: 0
Reputation: 1012
Difference between Checked and unchecked exceptions:
We have many differences between checked and unchecked exception but all the differences originate from once basic consideration that whether the exception is solvable by compiler or not.
Points to remember are:
[1] Checked exception means Compiler checked Exceptions. It means that compiler mandates that such exception to be handled by try-catch block or throws keyword.
[2] Unchecked exceptions are the ones for which compiler doesn’t provides any mandate as they can be resolved by developer by coding/programing as the control flow is controllable like in ArithmeticException, NullPointerException ArrayIndexOutOfBoundsException, IllegalArgumentException ,etc.
I call it “Exception-Identity-Test” where you take any random exception from java doc and just ask it one question. “Hey Exception! Can you be solved programmatically?”
If the exception says YES then it is an Unchecked Exception as this can be solved by either code change or resolving some calculation mistake etc.
On the other hand if the Exception says No then this is Checked Exception as in checked Exception control flow goes out of our code like if someone changes Database passwords or someone unplugs the network cable ,connection timeout (ConnectException), some resource is not found (FileNotFoundException, ClassNotFound), SQLException, InvocatonTargetException etc. These ones cannot be resolved by programming
Upvotes: 0
Reputation: 718718
TofuBeer's answer explains clearly what the exception classes mean.
Why these many types? Instead Java may simply follow a simple design(just try/catch all types) to handle an abnormal condition in a program?
Why? Because they are necessary! Without those 4 classes, handling exceptions by broad category would be impractical.
Error
class?Exception
class?RuntimeException
class?Upvotes: 4
Reputation: 992
Upvotes: 1
Reputation: 61526
Throwable is at the top off all exceptions. Underneath Throwable you have Error and Exception. Underneath Exception you have RuntimeException.
Java has two types of exceptions - checked and unchecked. Checked exceptions are enforced by the compiler (you have to declare them in the throws clause and catch them eventually). Unchecked exceptions are not enforced for catching or declaring in throws clause.
(Controversial part of the answer)
Throwable exists so that there is a parent for all exception types. You should never declare that you throw Throwable and never catch it (unless you really really really know what you are doing).
Error exists to indicate issues with the runtime environment, things that your program probably cannot recover from, such as a badly formatted class file or the VM running out of memory. You should not catch an Error unless you really know what you are doing.
Exception exists as the root for all non-programmer errors (see RuntimeException for the "exception" to this) , such as a file cannot be created because the disk is full. You should not throw, throws, or catch Exception. If you have to catch Exception make sure you know what you are doing.
RuntimeException exists to indicate all programmer error, such as going past the end of an array or calling a method on a null object. These are things that you should fix so that they do not throw exceptions - the indicate that you, the programmer, screwed up the code. Again, you should not catch these unless you know what you are doing.
Upvotes: 38
Reputation: 4997
Runtime Exceptions provide you with the flexibility to avoid catching, declaring the exceptions.
Upvotes: -1