"An exception is an event or condition which interrupted the normal flow of program execution.Which causes the program termination abnormally."
FileNotFoundException,ArithmeticException and NullPointerException etc.
Demo program based on ArithmeticException:
package com.navneet.javatutorial;
public class ExceptionIntroduction {
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();
int var2=0;
int c=var1/var2;
System.out.print(c);
m2();
}
public static void 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)
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 shows that the method m2() not executed and program terminates abnormally.
To avoid such conditions ,exception handling comes to the picture.
For more details click here
No comments:
Post a Comment