Creating an OutOfRangeException in Java

Description:

In this java code, we create an OutOfRangeException; and then we test it.

Code:

package com.javaproblems.creatingexceptions;

import java.util.Scanner;

public class CreatingExceptions {

 public static void main(String[] args) throws OutOfRangeException {
  Scanner scan = new Scanner(System.in);
  int val;
  final int MIN = 0, MAX = 100;
  OutOfRangeException problem = 
  new OutOfRangeException("Input value is not valid");
  System.out.println("Enter an int value between " +
                          MIN + " and " + MAX);
  val = scan.nextInt();
 
  if (val<MIN || val > MAX) {
   throw problem;
  }

 }

}
package com.javaproblems.creatingexceptions;

public class OutOfRangeException extends Exception{

 
 public OutOfRangeException(String msg) {
  super(msg);
  
 }

}

No comments:

Post a Comment