Beruflich Dokumente
Kultur Dokumente
In java,
object
exception is an
statement
statement
statement
statement
1;
2;
3;
4;
5.
6.
7.
8.
9.
10.
statement
statement
statement
statement
statement
statement
5;//exception occurs
6;
7;
8;
9;
10;
An exception can occur for many different reasons, below given are some
scenarios where exception occurs.
A network connection has been lost in the middle of communications or the JVM has
run out of memory.
Unchecked exception
Unchecked exceptions are not checked at compile time. It means if your program is
throwing an unchecked exception and even if you didnt handle/declare that exception,
the program wont give a compilation error.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked
exceptions are not checked at compile-time rather they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
int a=50/0;//ArithmeticException
class Example {
3.
4.
5.
int num1=10;
6.
int num2=0;
7.
8.
9.
int res=num1/num2;
10.
System.out.println(res);
11.
12. }
If you compile this code, it would compile successfully however when you will run it,
it would throw ArithmeticException. That clearly shows that unchecked exceptions
are not checked at compile-time, they are being checked at runtime
String s=null;
System.out.println(s.length());//NullPointerException
String s="abc";
int i=Integer.parseInt(s);//NumberFormatException
class Example {
public static void main(String args[])
{
try{
int arr[] ={1,2,3,4,5};
System.out.println(arr[7]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("The specified index does not exist " +
"in array. Please correct the error.");
}
}
}
try
2.
catch
3.
finally
4.
throw
5.
throws
try{
//code that may throw exception
}catch(Exception_class_Name ref){}
try{
//code that may throw exception
}finally{}