Exception Handling in Java:
---------------------------
The Exception Handling in Java is one of the powerful mechanism to handle the
runtime errors so that the normal flow of the application can be maintained.
Exception:-
------------
Exception is an abnormal condition. In Java, an exception is an event that disrupts
the normal flow of the program. It is an object which is thrown at runtime.
Throwable class is extend by Exception class and Error class.
There are mainly two type of Exception in java:-
1. Checked Exception
2. Unchecked Exception
1. Checked Exception : the class is directly inherited Throwable class expect
Runtime Exception and Error is known as Checked Exception .
Example : IOException, SQLException, ClassNotFound Exception .
2. Unchecked Exception : The classes that are inherit the runtime Exception are
known as Unchecked Exception.
Unchecked Exception are not checked at compile itme they are checked at runtime.
Example : ArithmethicException, NUllPointerException,
ArrayIndexOutOfBoundException.
Java Exception Keywords:
------------------------
try, catch, finally, throw, throws
1. try: try is use to specify a block where we should place our Exception [Link]
try block is used to enclose the code that might throw an exception.
Java try block must be followed by either catch or finally block.
2. catch : catch block is use to handle the [Link] block must be there.
At a time only one Exception occures and only one catch block will execute, and the
catch block must be ordered from most specific to most general.
3. finally: finally block is use to execute the necessary code or statements of a
[Link] execute whether an exception is handled or not.
3. throw: throw is use to throw an exception.
4. throws: throws is use with the method signature. it dose not handle the
exception it specify that there exception may occurs.
try:-
-------
-> try block is to enclose the code that may occurs exception.
-> with the try block must be use either catch block or finally block.
1.
try{}
catch(){}
2.
try{}
finally{}
-> must be use with methods.
-> in try block if exception occurs at the particular statement then program is
terminated and rest of the statement will not be execute, so it is recommended to
not keep the code that will not throw exception.
catch:-
-------
->catch block is use to handle the exception by declaring the type of exception
within the parameters .
-> We can use multiple catch block for a single try block.
-> it must be use after the try block only.
-> If exception is not handled then JVM provide the default exception handler that
perform these task,
*Prints out exception description.
*Prints the stack trace (Hierarchy of methods where the exception occurred).
*Causes the program to terminate.
Throwable
1. Exception 2. Error
1.1 Checked Exception
IOException
FileNotFoundException
SQLException
ClassNotFoundException
1.2 Unchecked Exception
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
ClassCastException
IllegalArgumentException
NumberFormatException
imp:- If inner try block not have any valid catch block to handle the exception
then the parent or outer catch block of parent try block is checked for the
exception if it match then the outer catch block is execute.
if none of them is unable to handle the exception then java runtime environement
handle the exception and provide the system generated message for the exception.
Finally block:-
--------------
It is use to execute the important code such closing connection and release the
resource .
Java Finally boock is always execute whether exception is handled or not .it
contain all the necessary statement to be execute.
closing the file , closing the connection.
-> For each try block there can be zero or only one finally block is there.
-> The finally block will not be execute if the program is exit using
[Link]().
Throw:-
--------
We can use the throw keyword to explicitly throw an [Link] Exception has
some message with it that provides the error description. These exceptions may be
related to user inputs, server, [Link] can throw either checked or unchecked
exceptions in Java by throw keyword. It is mainly used to throw a custom exception.
If we throw unchecked exception from a method, it is must to handle the exception
or declare in throws clause.
Points:-
-> "throws" clause is used in method header to declare the exception that the
method may throw. The method caller need to handle such exceptions using try-catch
block
->Checked exceptions must have try catch blocks or throws clause for compilation
->Checked exception must have try catch block or throws clause, it it is not there
compiler error will get.
->Cheched exception can not be [Link] can avoid unchecked exception.
-> Compiler checked only checked Exception.
Throws:-
---------