You are on page 1of 2

EXPERIMENT-4

ABHISHEK CHOUDHARY
07311503113
AIM
Exception Handling
(A) Write an program using try , catch and throw.
(B) Create your own exception class
(A) SOURCE CODE
class Exc
{
public static void main(String args[])
{
try
{
if(Integer.parseInt(args[0])<18)
{
throw new ArithmeticException("Not Valid");
}
else
{
System.out.println("Welcome");
}
}
catch(ArithmeticException e)
{
System.out.println(e);
}
}
}
OUTPUT

(B) SOURCE CODE


class MyException extends Exception
{
MyException(int a)
{
System.out.println("Not Valid : "+a+" is less than 10");
}
}
class Exc2
{
public static void main(String args[])
{
int i=Integer.parseInt(args[0]);
try
{
if(i<10)
{
throw new MyException(i);
}
else
{
System.out.println("Welcome");
}
}
catch(MyException e)
{
System.out.println("Caught " + e);
}
}
}
OUTPUT

You might also like