V.exception Handling
V.exception Handling
Lecture - 30
Did you notice the
gaps between two
rail tracks???
In
If summer,
there is no
duegapto
Why??? Thermal
heat,
between
iron the
will
Expansion
tracks,expand
it will bend
Accidents!!!
Result???
So, We have to rectify That’s why we are
that accidents having gaps between
the rail tracks
An exception is
handled here
Similarly in Java
How to handle
also we may get
it?????
problems
Exception
Abnormal condition
or not coming under
any rule
Exception in Java
• Runtime errors
• ClassNotFoundException,
• IOException,
• SQLException,
• RemoteException, etc.
Compile time error run time error
Error:
Output : ?????
C:\Sample>Javac Test.java
Syntax Errors Test.java:3: error: ';' expected
System.out.println("Hello")
public class Test
{
public static void main(String a[])
{
Run time error int arr[]=new int[5];
arr[10]=20;
}
}
Error:
Output
Exception in thread : ?????
"main"
logical Errors java.lang.ArrayIndexOutOfBoundsException: 10
at Test.main(Test.java:6)
Syntax Errors Errors
Exceptions
Checked Unchecked
Exceptions Exceptions
Checked Exception
• The classes which directly inherit Throwable class except
RuntimeException and Error are known as checked exceptions
• Example
• IOException,
• SQLException etc.
• Example
• ArithmeticException,
• NullPointerException,
• ArrayIndexOutOfBoundsException etc.
ArithmeticException
int a=50/0;
Common Scenarios of Java Exceptions
NullPointerException
String s=null;
System.out.println(s.length());
Common Scenarios of Java Exceptions
NumberFormatException
String s="abc";
int i=Integer.parseInt(s);
Common Scenarios of Java Exceptions
ArrayIndexOutOfBoundsException
catch
finally
throw
throws
try
{ System.out.println(e);
{ System.out.print(i+" ");
int n=s.nextInt(); }
catch block to catch
for(int i=0; i<=n; i++) }
the exception if
{ }
occurred
try
{
Calculation in try Output:
int data=100/i; java.lang.ArithmeticException: / by zero
block 0 1 2 3 4 5 6 7 8 9 10
}
Program with multiple catch
import java.util.*; catch(ArithmeticException e)
{ System.out.println(e);
{ catch(ArrayIndexOutOfBoundsException e)
for(int i=0;i<=n;i++) }
{ }
try } Output:
{ java.lang.ArithmeticException: / by zero
int data=100/i; java.lang.ArrayIndexOutOfBoundsException: 3
arr[i]=data;
java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 5
}
Java finally block
{ }
{ {
double data; {
{ else
{ }
data=100/i; }
} }
catch(ArithmeticException e) }
{
Output:
java.lang.ArithmeticException: / by zero
100.0
50.0
33.0
25.0
20.0
16.0
14.0
12.0
11.0
10.0
1 exception handled
Java throw keyword
throw
if(age<18)
else
System.out.println("welcome to vote");
}
Output:
public static void main(String args[])
Exception in thread "main"
{
java.lang.ArithmeticException: Under Age
Scanner s=new Scanner(System.in);
at Test.validate(Test.java:7)
int n=s.nextInt();
at Test.main(Test.java:15)
validate(n);
Command exited with non-zero status 1
}
}
Till now we discussed about
What if a checked
how to deal with unchecked throws
exception occurs???
exceptions
throws keyword
throws
Syntax
Example
class M m.method();
{ }