·
Whenever jvm executes program, it executes the
statement line by line. If
one of the statements generates abnormal condition then jvm terminates the execution.
·
When jvm finds abnormal condition and statement
it looks for throwable type class which defines the condition. Once it finds a
class create an instance of throwable and throws the object using “throws” keyword
·
Program has to catch the object in order to
continue the execution. Otherwise the program will be terminated
·
To catch the exception object, java provides
“try and catch” block, try and catch should be written together
·
Try block should contains the statements which
may generates an exception and catch block should have argument of type
throwable
·
Whenever an exception occur in try block, the
corresponding catch block gets executed and program will continue the execution
and try and catch block
·
If there is no exceptions in try block, then
catch block will not be executed
·
A try block can have multiple catch block, in
such cases catch block matching will happen in sequential order but only one
catch block will be executed
·
If we have to develop single catch block which
can handle any type of error and exception then the catch block argument should
be throwable type
·
The exception is categorised into 2 category
(1)
Checked Exception
(2)
Unchecked Exception
·
Any exception type which a compiler can identify
at the compilation time, these exceptions are known as Checked Exception. For e.g. Exception classes which are subclass to
the exception type are comes under Checked Exception like I/O, SQL, File not
found exception etc
·
Any exception which compiler fails to identify
at the time of compilation are known as Unchecked
Exception. For e.g. all classes which are subclass to error and subclass to
runtime exception are categorised as Unchecked
Exception
·
Unreachable
Code: code which is not used. For e.g. when we use throwable
catch(Throwable exp), it will not allow other catch to execute
·
Finally
Block: Finally block gets executed irrespective of exception
occurrence. Whenever we want to execute mandatory statement then we go for
finally block. For e.g. opening the connection and closing the connection (closing
stream of file) etc
No comments:
Post a Comment