Exception Handling !! How JAVA will "Try" to "Catch" the Exception :)

The majority of the learners are trying to get Exception and the best possible methods of taking care of them. The primary goal of this little article is to give some understanding on exception handling with some of coding models.
What is an Exception ?

An Exception is a strange condition that emerges in a code arrangement.
In other scripting languages that don't support exception handling dealing with, errors occuring must be checked and handle physically through the error codes.
Exemptions can be dealt with by the software engineer (try- catch) or it is handle by the java environment(throws).

A few Examples


Here the length of the array is 6 at that point last index value will be 5 (length — 1) yet for circle emphasizes until sixth record.
public class ExceptionOne{
public static void main(String[] arg){
int[] arr = {3,1, 2, 7,9,4};
for(int y=0; x<7; x++){

2. Java.lang.NullPointerException

employee type array is instated with the size of four yet without introducing zeroth file employee object designer trys to appoint values for name and age qualities.
class Employee{
int age;
String name;
public class ExceptionTwo{
public static void main(String[] arg){
Employee [] employee = new Student[4];
employee [0].age = 31;
employee [0].name= "Saini";

3. Java.lang.ArithmaticException:/by zero

Administrator may not understand 46/an statement since a = 0 and translator can't find precise solution for 56/a.

public class ExceptionDemoThree{
public static void main(String[] arg){
int a =0;
float b = 46/a;

Kinds of Exception

Complie Time Exception — CheckedException

In the event that these exceptions are not taken care of/proclaimed in the program, you will get compilation error.


Run Time Exception — UncheckedException

These exception handlings are not checked at compile time so compiler doesn't check whether the software engineer has taken care of them or not however it's the duty of the developer to deal with these exemptions and give a protected exit.


What is an Error ?

These are not exception handlings by any stretch of the imagination, however issues that emerge outside the ability to control of the client or the software engineer. Errors are commonly overlooked in your code since you can infrequently take care of a them. For instance, if a stack flood happens, a error will emerge. They are likewise overlooked at the hour of arrangement.

How would we handle Exception ?

Exemption dealing with is practiced through the "try-catch" system, or by "throws" proviso in the method revelation.

For any code that throws a checked exception handling, you can choose to deal with the exemption yourself, or pass the exception handling "up the chain" (to a parent class).

To deal with the exemption, you compose an "try-catch" block. To pass the exception handling "up the chain", you announce a "throws" condition in your method or class revelation.

On the off chance that the method contains code that may cause a checked exception handling, you MUST deal with the exemption OR pass the exception handling to the parent class.

Try-Catch Mechanism

Try block

The try block contains set of articulations where a exception handling can happen. An try block is constantly catch by a catch block, which handles the exemption that happens in related try block. An try block should be catch by get blocks or at long last block or both.

Catch block

A catch block is the place you handle the exception handlings, this block should follow the try block. A single try block can have a few catch blocks related with it. You can get various exception handlings in various catch blocks. At the point when a exception handling happens in try hinder, the relating get obstruct that handles that specific exemption executes. For instance in the event that a Arithmetic exception handling happens in try block, at that point the stateements encased in catch block for number-crunching exemption executes.

Finally block

You can connect an at long last statement to an try block.

The code inside the at last provision will consistently be executed, regardless of whether an exemption is tossed from inside the try or catch block.

In the event that your code has an arrival proclamation inside the try or catch hinder, the code inside the at long last block will get executed before coming back from the strategy.

Exception & Call Stack

               At the point when a exception happens inside a Java method, the method makes an Exception item and passes the Exception object to the JVM (in Java term, the strategy "throw" an Exception). The Exception object contains the sort of the exception, and the condition of the program when the exemption happens. The JVM is liable for finding a special case handler to process the Exception object. It look through in reverse through the call stack until it finds a coordinating special case handler for that specific class of Exception object (in Java term, it is designated "get" the Exception). In the event that the JVM can't locate a coordinating special case handler in all the techniques in the call stack, it ends the program.

This procedure is outlined as follows. Assume that methodD() experiences an irregular condition and tosses a XxxException to the JVM. The JVM look through in reverse through the call stack for a coordinating special case handler. It discovers methodA() having a XxxException handler and passes the exemption article to the handler. Notice that methodC() and methodB() are required to announce "tosses XxxException" in their strategy marks so as to assemble the program.      


I figure this article will assist you with getting fundamental comprehension on exception handling dealing with.