blog

Home / DeveloperSection / Blogs / Exception Handling in java

Exception Handling in java

Anonymous User 846 04-Sep-2018

EXCEPTION HANDLING

• INTRODUCTION

• Runtime STACK MECHANISM

• DEFAULT EXCEPTION HANDLING

• EXCEPTION HIERARCHY

• CONTROL FLOW IN TRY CATCH

EXCEPTION: the unwanted, unexpected event that affects the normal flow of a program is known as exception. e.g. Filenotfound Exception.

We have to handle the exception for the normal termination of the program. Exception handling is the mechanism of providing alternative way to continue the rest of the program.

E.g.

Try{

Read data from london file
}
Catch(filenotfound exception)
{
Use local file and continue the rest of the program
}

Runtime stack mechanism:

• For every thread, a runtime mechanism is created in java.

• All the method call performed by the thread will be stored in stack.

• Each entry in the store is called activation record.

• After completing every method call jvm deletes corresponding entry from the stack

• And after completing all method call jvm destroys the stack.

Default exception handling:

If any exception is raised in a method then that method is responsible for creating an exception object for that exception and that exception should contain the following:

1. Name

2. Description

3. Location

After this exception object will be handover to the jvm. Now jvm checks whether the method contains exception handling code then it will be executed and rest of the program normally .if it does not contain any code then JVM terminates the method abnormally and remove the corresponding stack entry from the code. Now jvm checks the caller method whether it contains the handling code or nor if it does not then user terminates the caller method abnormally and removes corresponding entry from the stack. This process continues until the main and if main also does not contain code the program will terminate abnormally. before program terminates abnormally jvm handovers the responsibility of exception handling to the default exception handler.

Default exception handler just print exception information to the console in following format.

                             Name of exception: Description

                                  Location (stack area)

EXCEPTION HIERARCHY:

The root of exception hierarchy is throwable class.

It has following two child concept:

1. Exception

2. Error

Exception: most of the cases exceptions are caused by our program and these are recoverable.

Error: Errors are caused by due to lack of system resources.it is non-recoverable.

Checked vs unchecked Exception:

Checked exception:

The exception which is checked by a compiler at runtime is known as a checked exception. e.g.

FileNotFound exception.

Unchecked exception: The exception which is not checked by a compiler is known as unchecked exception e.g. Arithmetic exception

Control flow in try catch:

 Case 1: Exception occurs in a try block and handled in a catch block

 Case 2: Exception occurs in try-block is not handled in a catch block

 Case 3: Exception doesn’t occur in try-block



Updated 04-Sep-2018
I am a content writter !

Leave Comment

Comments

Liked By