Aim:- Write a program to
implement Custom Exception in java.
JAVA Code :-
import java.lang.*;
class MyException extends
Exception
{
int
a;
MyException(int
b)
{
a=b;
}
public
String toString()
{
return
"Hello its Custom Exception";
}
}
class CustomException
{
static
void compute(int a) throws MyException
{
if(a>10)
{
throw
new MyException(a);
}
System.out.println("Normal
exit");
}
public
static void main(String args[])
{
try
{
compute(1);
compute(11);
}
catch (MyException e)
{
System.out.println("Caught:
" + e);
}
}
}
Output:-
No comments:
Post a Comment