Handling Exceptions in JAVA

Define Exception :-

=>The disturbance which is occured from the application is known as Exception.

Define Error :-

=>The disturbance which is occured from the environment is known a Error.

Define Exception Handling process :-

=>The process which is used to handle the exception is known as Exception Handling process.

=>The following are the blocks used in constructing Exception Handling process:

1.try block

2.catch block

3.finally block

1.try block:

=>try block will hold the set-of-statements which are going to raise the exception.

syntax:

try

{

     //set-of-statements;

}

Note:

=>when the exception is raised in try block, one object is created for Exception_type_class and the reference of object is thrown onto catch block.

2.catch block:

=>catch block will hold the reference of object thrown from the try block and the required msgs are generated from the catch block.

syntax:

catch(Exception_type_class ref_var)

{

      //msg

}

3.finally block:

=>finally block is part of exception handling process,but executed independently without depending on exception.

syntax:

finally

{

     //statements

}

Note:

=>In realtime finally blocks are used to hold resource closing operations. (IO Closing,File Closing,DB Closing… )

*imp

Define ‘Throwable’ class

=>’Throwable’ class is from ‘java.lang’ package and which is root of exception handling process.

=>This ‘Throwable’ class is extended into the following two SubClasses:

1.Error class

2.Exception class

Hierarchy of ‘Throwable’ class:

1.Error class:

=>’Error’ class is the SuperClass or PClass of all the disturbances occured from the

Environment.

Note:

=>There is no separate process to handle the errors.

2.Exception class:

=>’Exception’ class is the SuperClass or PClass of all the disturbances occured from the application.

=>The Exceptions are categorized into two types:

(a)Unchecked Exceptions

(b)Checked Exceptions

(a)Unchecked Exceptions:

=>The exceptions which are not identified by the compiler at compilation stage will

be raised at execution stage are known as UnChecked exceptions or Runtime exception.

=>These UnChecked exceptions are categorized into two types:

(i)Built-In Unchecked exceptions

(ii)User defined UnChecked exceptions

(i)Built-In Unchecked exceptions:

=>The UnChecked exceptions which are available from JavaLib are known as Built-In

UnChecked Exceptions or Pre-Defined UnChecked exceptions.

Exp:

java.lang.ArithmeticException

java.util.InputMismathException

java.lang.NumberFormatException

Note:

=>when these Built-in UnChecked exceptions are raised,then the executionControl

will create object for Exception_type_class and throw the reference onto catch

automatically.

(ii)User defined UnChecked exceptions:

=>The process of defining and raising UnChecked exceptions are known as ‘User defined

UnChecked exceptions’

Note:

=>In User defined UnChecked exceptions,we have to create object for the class from

where the exception is raised and throw the reference on to catch block using ‘throw’

keyword.

(b)Checked Exceptions:

=>The exceptions which are identified by the compiler at compilation stage are known as Checked Exceptions or Compile time exceptions.

=>These Checked Exceptions are categorized into two types:

(i)Built-In Checked Exceptions

(ii)User defined Checked Exceptions

(i)Built-In Checked Exceptions:

=>The Checked exceptions which are available from JavaLib are known as Built-In checked exception or PreDefined Checked exceptions.

Exp:

java.lang.InterruptedException

java.lang.ClassNotFoundException

java.io.IOException

Note:

=>The ‘throws’ keyword added with method signature specify to ignore the exception (disturbance) from the current method and raise at method_class.

=>when the exception is raised at method_call then it is identified by the compiler at compilation stage,because of this reason it comes under CompileTime exception or Checked Exception.

faq:

wt is the diff b/w

(i)throw

(ii)throws

=>’throw’ keyword is used to throw the reference from the try block to catch block.

=>’throws’ keyword is used to ignore the exception from current method and raise

at method call.

(ii)User defined Checked Exceptions:

=>The Checked exceptions which are defined and raised by the programmer are known as User defined Checked Exception.

Note:

=>In the process of defining and raising “User defined checked exceptions”, we use ‘throw’ keyword part of catch block to perform re-throwing process and we also declare ‘throws’ keyword with method_signature to ignore the exception in current method and raise at method_call.

Leave a Comment

Your email address will not be published. Required fields are marked *