Uttar Pradesh, India
Instagram
image

An introduction to Exception Handling

Exception:

"An exception is an event or condition which interrupted the normal flow of program execution.Which causes the program termination abnormally." Example : FileNotFoundException,ArithmeticException and NullPointerException etc.

Demo program based on ArithmeticException:

package com.navneet.javatutorial;
public class ExceptionIntroduction {

public static void main(String[] args) {

m1();
}

public static void m1(){

int var1=12;
int var2=0;
int c=var1/var2;
System.out.print(c);
m2();

}
public static void m2(){

System.out.print("Method m2 statement");

}

}
Console Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.navneet.javatutorial.ExceptionIntroduction.m1(ExceptionIntroduction.java:16)
at com.navneet.javatutorial.ExceptionIntroduction.main(ExceptionIntroduction.java:6)

 

The last two lines show that the method m2() not executed and the program terminates abnormally.

To avoid such conditions, exception handling comes to the picture.


Comments

Share your thoughts in the comments

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