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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact