Aim:
To write a java program to implement user defined exception handling
Procedure:
- Create a class that extends the Exception class.
- Create a constructor which receives the string as an argument.
- Get the Amount as input from the user.
- If the amount is negative, the exception will be generated.
- Using the exception handling mechanism, the thrown exception is handled by the catch construct.
- After the exception is handled, the string “invalid amount “ will be displayed.
- If the amount is greater than 0, the message “Amount Deposited “ will be displayed
Program 1:
import java.util.Scanner; class NegativeAmtException extends Exception { String msg; NegativeAmtException(String msg) { this.msg=msg; } public String toString() { 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.nextInt(); try { if(a<0) { throw new NegativeAmtException("Invalid Amount"); } System.out.println("Amount Deposited"); } catch(NegativeAmtException e) { System.out.println(e); } } }
Output:

Program 2:
class MyException extends Exception{ String str1; MyException(String str2) { str1=str2; } public String toString() { return ("MyException Occurred: "+str1) ; } } class example { public static void main(String args[]) { try { System.out.println("Starting of try block"); throw new MyException("This is My error Message"); } catch(MyException exp) { System.out.println("Catch Block") ; System.out.println(exp) ; } }}
Output:

Result:
Thus a java program to implement user defined exception handling has been implemented and executed successfully.