Java exception handling is a Java
application that helps to handle the error occurred in different phases.
Whenever any error occurs an exception is thrown. Java exceptions are the
errors that occur while executing a program and these occurs for different
reason like when any invalid data entered as input, for a non-existing file is
asked to access etc. Java exception is either generated by the JVM (Java
Virtual Machine) or it is generated by the code resultant from a throw
statement. Exception is an inbuilt class in the default package known as
java.lang. The exception handling is a significant part of Java programming.
Types of Exception
There are two types of exception
1. Checked
Exception
2. Unchecked
Exception.
Checked exception
A checked exception is any subclass of Exception (or Exception itself), excluding the class RuntimeException and all its subclasses. These exceptions cannot be ignored at the time of compilation and the programmers need to deal with the prospect of the exception that will be thrown.
A checked exception is any subclass of Exception (or Exception itself), excluding the class RuntimeException and all its subclasses. These exceptions cannot be ignored at the time of compilation and the programmers need to deal with the prospect of the exception that will be thrown.
Examples of Checked Exception:
IOException
SQLException
Unchecked exception
Unchecked Exceptions mainly occur for
the different programming errors such as accessing a null object
(NullPointerException), accessing an element of an array that is out of bounds
(ArrayBoundExceptions). Unchecked Exception is direct sub Class of
RuntimeException.
Exception Hierarchy
The super class for exceptions is
Throwable and it contains two subclasses Error and Exception. Every class in
the hierarchy suffixes with Exception, but Error does not. All the exceptions
are caused by problems within software
but Error is caused by hardware problems, The above of exceptions can be
divided in two categories – unchecked exceptions and checked exceptions.
Throw
Throw is used to actually throw the
exception. It is used to raise exception explicitly that means it is use when a
user defined exception is raised.
Throws
Throws keyword, used to declare an
exception. It gives the information regarding the exception to the programmer
so the programmer can provide an exception handling code. They are not
interchangeable.
Try block
A try statement is used to catch
exceptions that might be thrown by an executed program. Whenever, a try block
is in use a statement that might throw an exception and that prevent from
program crashing if any exception occurs.
Catch Block
The catch block contains a series of
legal Java statements. These statements are executed if and when the exception
handler is invoked. The runtime system invokes the exception handler when the
handler is the first one in the call stack whose type matches that of the
exception thrown.
Finally
When a try block runs till the end,
and there are no exception is
Thrown then the finally block will be
executed after the try block but when an exception is thrown by the try block
and it caught in one of the catch blocks the finally block will execute after
the catch block.
Difference between Errors and
Exception
Exceptions:
An exception has two
subclasses, checked and unchecked. The Exception is
point out an error that may cause by the programmer. An Exception handled at the application
level
|
Errors:
Errors are generated to indicate
errors generated by the runtime environment. Errors don't have any subclasses
while, tare always unchecked. Errors can be lethal in nature and revival from Error often is not possible. Usually
indicate a system error or a problem with a low-level resource. Errors should
be handled at the system level.
|
Example of Some Checked and Unchecked Exceptions
Checked
IOException
InterruptedException
NoSuchMethodException
ClassNotFoundException
CloneNotSupportedException
FileNotFoundException
ParseException
InstantiationException
|
Unchecked
NullPointerException
StackOverflowError
ArrayIndexOutOfBoundsException
IllegalArgumentException
ExceptionInInitializerError
IllegalStateException
NumberFormatException
ClassCastException
NoClassDefFoundError
|
No comments:
Post a Comment