Managing Errors and Exception: by Prof - Manikandan QMC College, Medavakkam
Managing Errors and Exception: by Prof - Manikandan QMC College, Medavakkam
AND EXCEPTION
By
Prof.Manikandan
ERRORS
An errors may produce an incorrect output or
may terminate the execution of the program.
Types of Errors:
1. Compile time errors
2. Run time errors
EXCEPTION HANDLING
The exception handling is one of the powerful
mechanism provided in java.
It provides the mechanism to handle the runtime
errors so that normal flow of the application can
be maintained.
Exception:
Dictionary Meaning: Exception is an abnormal
condition.
In java, exception is an event that disrupts the
normal flow of the program.
It is an object which is thrown at runtime.
TYPES OF EXCEPTION
1.
2.
Checked Exception
The
classes
that
extend
Throwable
class
except
RuntimeException and Error are known as checked exceptions
e.g. IOException, SQLException etc.
Unchecked Exception
e.g.ArithmeticException,NullPointerException,
ArrayIndexOutOfBoundsException etc.
1. ArithmeticException occurs
If we divide any number by zero, there occurs an
ArithmeticException.
inta=50/0; //ArithmeticException
2. NullPointerException occurs
Strings=null;
System.out.println(s.length());//NullPointerException
3. NumberFormatException occurs
Strings="abc";
inti=Integer.parseInt(s);//NumberFormatException
4. ArrayIndexOutOfBoundsException occurs
inta[]=newint[5];
a[10]=50;//ArrayIndexOutOfBoundsException
1.
2.
3.
4.
5.
TRY BLOCK
Enclose the code that might throw an exception in try block. It must
be used within the method and must be followed by either catch or
finally block.
CATCH BLOCK
Catch block is used to handle the Exception. It must be used after the try block.
Example:
classSimple{
publicstaticvoidmain(Stringargs[])
{
try
{
intdata=50/0;
}
catch(ArithmeticExceptione)
{
System.out.println(e);
}
System.out.println("restofthecode...");
}
}
Output:Exception in thread main java.lang.ArithmeticException:/ by zero rest of the
code...
classSimple{
publicstaticvoidmain(Stringargs[])
{
intdata=50/0;
System.out.println("restofthecode...");
}
}
Output:Exception in thread main
java.lang.ArithmeticException:/ by zero
you have to perform different tasks at the occurrence of different Exceptions, use multiple catch block.
classExcep4
{
publicstaticvoidmain(Stringargs[])
{
Try
{
inta[]=newint[5];a[5]=30/0;
}
catch(ArithmeticExceptione)
{
System.out.println("task1iscompleted");
}
catch(ArrayIndexOutOfBoundsExceptione)
{
System.out.println("task2completed");
}
catch(Exceptione)
{
System.out.println("commontaskcompleted");
}
System.out.println("restofthecode...");
}}
Output:task1 completed rest of the code...
FINALLY BLOCK
classSimple
{
publicstaticvoidmain(Stringargs[])
{
try
{
intdata=25/0;
System.out.println(data);
}
catch(ArithmeticExceptione)
{
System.out.println(e);
}
finally
{
System.out.println("finallyblockisalwaysexecuted");
}
System.out.println("restofthecode...");
}
}
Output:Exception in thread main java.lang.ArithmeticException:/ by zero finally block is
always executed rest of the code...
THROW KEYWORD
classExcep13
{
staticvoidvalidate(intage)
{
if(age<18)
thrownewArithmeticException("notvalid");
else
System.out.println("welcometovote");
}
publicstaticvoidmain(Stringargs[])
{
validate(13);
System.out.println("restofthecode...");
}
}
Output:Exception in thread main java.lang.ArithmeticException:not
valid
import java.lang.Exception;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
class TestMyException
{
public static void main(String args[])
{
int x=5, y=1000;
try
{
float z=(float)x/(float)y;
if(z<0.01)
{
throw new MyException("Number is too small");
}
}
catch (MyException e)
{
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("I am always here");
}
}
}
OUTPUT
Output: Caught my exception
Number is too small
I am always here