Common Exceptions in Java:
All Exceptions are divided into two categories. They are
a) JVM Exceptions:
Raised automatically by the JVM when ever a particular condition occurs.
Ex: ArithmeticException, NullPointerException.
b) ProgramaticExceptions:
These are raised programmatically because of programmers code or API’s developers Code.
Ex: IllegalArgumentException, NumberFormatException
JVM Exceptions with example:
1) ArrayIndexOutOfBoundsException:-
It is the child class of RuntimeException and it is unchecked thrown automatically by the JVM when ever we are accessing an array element with invalid int index.(Out of range index)
Ex:
int [] a = {10, 20, 30};
System.out.println(a[0]);
System.out.println(a[20]); // ArrayIndexOutOfBoundsException
System.out.println(a[20]); // ArrayIndexOutOfBoundsException
2) NullPointerException:-
It is the direct child class of RuntimeException and it is unchecked. Thrown automatically by the JVM when ever we are performing any operation on null.
Ex:
String s = null;
System.out.println(s.length()); //NullPointerException
3)ClassCastException:-
It is the child class of RuntimeException and it is unchecked. Thrown automatically by the JVM when ever we are trying to typecast parent class object to the child type.
Ex:
String s = "raju";
Object o = (Object)s;
Object o = new Object();
String s = (String)o;
R.E: ClassCastException
4)IllegalArgumentException:-
It is the child class of RuntimeException and it is unchecked thrown explicitly by the programmer or API developer when ever we are invoking a method with inappropriate or invalid argument.
Ex:
The valid range of Thread priority is 1 – 10 , if we are trying to invoke setpriority method with 100 as argument we will get IllegalArgumentException.
public void setPriority(int i){
if (i>10 || i<11)
{
throw new IllegalArgumentException();
}
}
5)NumberFormatException:-
It is the child class of Illegal Argument and it is unchecked. Thrown programmatically when ever we are attempting to convert String to Number type but the String is not formatted properly
Ex:
String s = 10;
int i = Integer.parseInt(s);
String s = "ten";
int i = Integer.parseInt(s); // NullPointerException
6)IlleaglStateExcepiton:-
It is the child class of RuntimeException and it is unchecked. Thrown programmatically to indicate that a method has invoked at an inappropriate time
Ex:
- After invalidating a session object we are not allowed to call any method on that object other wise IllegalStateException.
- After comiting the response we are not allowed to redirect or forward otherwise IlleagalStateException
- After starting a thread we are not allowed to start the same thread once again otherwise IlleagalStateException.
MyThread t = new MyThread();
t.start();
t.start(); // IllegalThreadStateException
Now Programatic Exception In Java:
7)NoClassDefFoundError:-
It is the child class of Error and it is unchecked. Thrown automatically by the JVM if the required .class file is not available.
Ex: java Test
if Test.class file is not available we will get NoClassDefFoundError.
8) StackOverFlowError:-
It is the child class of Error and it is unchecked. Raised automatically by the JVM when ever we are performing recursive method invocation.
Ex:
class Test
class Test
{
public static void m1();
{
{
m1();
}
public static void main(String arg[])
{
{
m1();
}
}
}
9) ExceptionInInitializerError:-
It is child class of Error and it is unchecked. Raised automatically by the JVM when ever an Exception occur during initialization of static variables or execution of static blocks.
Ex:
class Test{
class Test{
static int i = 10/0;
}
Output:
class Test{
static{
String s = null;
System.out.println(s.length());
System.out.println(s.length());
}
}
Output:-
Output:-
10) Assertion Error:-
It is the child class of Error and it is unchecked thrown programmatically to indicate
Assertion fails.
Ex:
Assert(false); // AssertionError
nice explanation for exceptions in java
ReplyDelete