Top 10 Exception in Java
Remember the biggest difference between
checked and unchecked exceptions is that checked exceptions are forced by compiler and used to indicate exceptional conditions that are out of the control of the program (for example, I/O errors), while unchecked exceptions are occurred during runtime and used to indicate ...
All Exception divided into two type
1-JVM Exception
2-Programatic Exception
1-ArrayIndexOutOfBoundsException
* it is child class of RuntimeException it is unchecked Exception.
* it is automatically raised by JVM .
whenever we are trying to access Array Element out of range
public static void main(String[] args) {
int [] array = new int [10];
System.out.println(array[0]);
System.out.println(array[100]);}
output
0
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 100
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:28)
2- NullPointerException
- it is child class of RuntimeException it is unchecked Exception.
- it is automatically raised by JVM .
- when ever we are trying to perform any opration on Null
String action = null;
System.out.println(action);
System.out.println(action.length());
outputnull
Exception in thread “main” java.lang.NullPointerException
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:26)
3-StackOverFlowError
- it is a child class of error and it is unchecked .
- When you recursly method invocation then you will get StackOverFlowError .
public class ArraysDS {
public void m1() {
System.out.println(“m1”);
m2();}
public void m2() {
System.out.println(“m2”);
m1();}
public static void main(String[] args) {
ArraysDS a = new ArraysDS();
a.m1();
a.m2();
}
output
m1
m2
m1
m2
Exception in thread “main” java.lang.StackOverflowError
at java.io.FileOutputStream.write(FileOutputStream.java:326)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at java.io.PrintStream.write(PrintStream.java:482)
4- NoClassDefFoundError
- when Java Virtual Machine is not able to find a particular class at runtime
- it is a child class of error and it is unchecked .
- it is automatically raised by JVM .
class Desktop{main()}
javac Desktop.java — compiling progaram
After Compilation ….Java Desktop — Run program
if Desktop.class file not available then it will return Runtime Exception NoClassDefFoundError
5-ClassCastException
- It is child class of RuntimeException it is unchecked Exception.
- It is automatically raised by JVM .
- When ever we are trying to typecast parent to child type.
String s = new String(“Baba”);
Object o = s;
System.out.println(o);
Object x = new Object();
String y = (String) x;
System.out.println(y);
output
Baba
Exception in thread “main” java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:29)
6- ExceptionInInitializerError
- if any exception occurs while performing initilazation for static variable and while executing static blocks
- It is automatically raised by JVM .
public class ArraysDS {
static int x = 10 / 0;
public static void main(String[] args) {
System.out.println(x);}}Exception in thread “main” java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
at org.practice.datastructure.problem.ArraysDS.<clinit>(ArraysDS.java:12)
public class ArraysDS {
static {
String x = null;
System.out.println(x.length());}
public static void main(String[] args) {}}
Exception in thread “main” java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
at org.practice.datastructure.problem.ArraysDS.<clinit>(ArraysDS.java:12)
7-IllegalArgumentException
- This exception indicates that a method is called with incorrect input arguments.
- Raised explicitly by programmer or by API developer
public class ArraysDS {
public static void main(String[] args) {
Thread t1 = new Thread();
t1.setPriority(10);
t1.setPriority(100);}}Exception in thread “main” java.lang.IllegalArgumentException
at java.lang.Thread.setPriority(Thread.java:1089)
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:15)
8- NumberFormatException
- when you try to convert String to Number type but String is properly formatted.
- Raised explicitly by programmer or by API developer
public class ArraysDS {
public static void main(String[] args) {
int i = Integer.parseInt(“10”);
System.out.println(i);
int x = Integer.parseInt(“Ten”);
System.out.println(x);}}
output
10
Exception in thread “main” java.lang.NumberFormatException: For input string: “Ten”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:15)
Handle By Try Catch with custom message
public class Difference {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String S = in.next();
try {
int x = Integer.parseInt(S);
System.out.println(x);}
catch (NumberFormatException e) {
System.out.println(“Box”);}}}
9 -IllegalStateException
- Raised explicitly by programmer or by API developer
- Once session expire we can’t call any method on that object otherwise we will get IllegalStateException.
HttpSession session = request.getSession();
System.out.println(session.getId()); — — — — — — — - o/p — — 121
session.invalidate();
System.out.println(session.getId()); — — — — o/p IllegalStateException
public class ArraysDS {
public static void main(String[] args) {
Thread t1 = new Thread();
t1.start();
t1.start();
System.out.println();}}
o/p
Exception in thread “main” java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:16)
10 -AssertionError
- it is a child class of error ,hence it is unchecked.
- Raised explicitly by programmer or by API developer.
- when you assert statement is fail then you will get AssertionError.
public class ArraysDS {
public static void main( String args[] ){
Scanner scanner = new Scanner( System.in );
System.out.print(“Enter ur age “);
int value = scanner.nextInt();
assert value>=18:” Not valid”;
System.out.println(“value is “+value);}}
Enter ur age 1
Exception in thread “main” java.lang.AssertionError: Not valid
at org.practice.datastructure.problem.ArraysDS.main(ArraysDS.java:21)
Note -It will not run simply because assertion is disabled by default. To enable the assertion, -ea
Basic Notes