oops 9
userdefined.java
import java.util.*;
classNegativeAmt Exception extends Exception
{
string msg;
NegativeAmt Exception(string msg)
{
this.msg=msg;
}
public string to string()
{
return msg;
}
}
public class userdefined
{
public static void main(string[]args)
{
Scanner s=new Scanner(system.in);
system.out.print("Enter Amount:");
int a=s.next();
try
{
if(a<0)
{
throw new negative Amt Exception("Invalid Amount");
}
system.out.println("Amount Deposited");
}
catch(Negative Amt Exception e)
{
system.out.println(e)
}
}
}
class My Exception extends Exception
{
string str 1;
My exception(string str 2)
{
str 1=str 2;
}
public string to string()
{
return("My Exception occured:"+str 1);
}
}
class Example
{
public static void main (string args[])
{
try
{
system.out.println("starting of try block");
throw new My Exception("this is my error message");
}
catch(My Exception exp)
{
system.out.println("catch block");
system.out.println(exp);
}
}
}