Tuesday, 29 August 2017

Java Programming tutorial to demonstrates the use of throw keyword in exception handling

WAP that creates a custom exception InvalidAgeException which will be thrown when user enters age below 18

Program

import java.util.Scanner;
public class Custom_exception{
          static void validate(int age){ 
           if(age<18) 
            throw new ArithmeticException("not valid"); 
           else 
            System.out.println("welcome to vote"); 
   } 
   public static void main(String args[]){
            int i;
                   Scanner input = new Scanner(System.in);
                   System.out.println("Enter Your age");
                   i = input.nextInt();

            validate(i);           }  }

Output

Custom exception  handling Core Java

0 comments:

Post a Comment