Advertisement
Shailrshah

Creating a Custom Exception

Sep 14th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. import java.util.Scanner;
  2. class MyException extends Exception{}
  3. class InputTest{
  4.         static Scanner sc = new Scanner(System.in);
  5.         static public void main (String[] args){
  6.                 System.out.print("Enter your favorite number: ");
  7.                 try{
  8.                         if(!sc.hasNextBigInteger() && !sc.hasNextBigDecimal()){
  9.                                 MyException e = new MyException();
  10.                                 throw e;
  11.                         }
  12.                         else    System.out.println("That's a cool number!");
  13.                 } catch(MyException e){
  14.                         System.out.println("That's not really a number!");
  15.                 }
  16.         }
  17. }
  18. //Output:-
  19. //C:\Java>java InputTest
  20. //Enter your favorite number: 3.1415926535897932384626433832795028841971693993751058209
  21. //749445923078164062862089986280348253421170679
  22. //That's a cool number!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement