Exception Handling In Java
Chapter:
Exception Handling
Last Updated:
24-03-2017 09:02:17 UTC
Program:
/* ............... START ............... */
public class JavaExceptionExample {
public static void main(String args[]) {
String s = "abc";
int i = Integer.parseInt(s);// NumberFormatException
int a[] = new int[5];
a[10] = 50; // ArrayIndexOutOfBoundsException
String str = null;
System.out.println(str.length());// NullPointerException
int abc = 50 / 0;// ArithmeticException
}
}
/* ............... END ............... */
Notes:
-
The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
- Exception handling helps programmer to handle runtime errors so that the normal flow of the application can be maintained.
- An exception can occur due to many different reasons, some of them are:
- A user has entered invalid data.
- A file that needs to be opened cannot be found.
- A network connection has been lost in the middle of communications or the JVM has run out of memory.
- In java, exception is an event that disrupts the normal flow of the program. It is an object, which is thrown at runtime.
- The main advantage of exception handling is that normal flow of the application is maintained. Normally Exception disrupts(make a break in) the normal flow of the application that is why we use exception handling in java.
Tags
Exception Handling, Java, Exception Handling