Custom Exception Example in Java

import java.util.Scanner;

class MarriageAgeException extends Exception {
 public MarriageAgeException(String message) {
  super(message);
 }
}
public class MyOwnException {

 public static void main(String args[]) throws MarriageAgeException {

  Scanner sc = new Scanner(System.in);

  System.out.println("Enter a person age");

  int age = sc.nextInt();
  if (age <= 30) {
   
   System.out.println("Valid for Marriage");
  
  } else {
   
   throw new MarriageAgeException("Maarige Age is Over Exception");
  
  }
 }
}

Search This Blog

All the rights are reserved to this blog is belongs to me only.. Powered by Blogger.