2011-05-01 250 views
0

我試圖在用戶輸入失敗時拋出異常TooLongEx失敗。一直停留在這個永遠:(拋出異常

import java.util.Scanner; 
public class MessageTooLong extends Exception { 
    public static void main(String args[]) 
    throws TooLongEx { 
     Scanner keyboard = new Scanner(System.in); 
     String line; 
     char preference; 
     int length; 
     boolean go = true; 
     while (go) { 
      System.out.println("Enter a line of text."); 
      System.out.println("Use no more than 20 characters."); 
      line = keyboard.next(); 
      length = line.length(); 
      if (length <= 20) { 
        System.out.println("You entered " + length + " characters, which is an acceptable length."); 
        System.out.println("Would you like to enter another line?"); 
        System.out.println("Enter 'y' to continue or 'n' to quit."); 
        preference = keyboard.next().charAt(0); 
       if ((preference == 'y') || (preference == 'Y')) { 
        go = true; 
       } else { 
        go = false; 
       } 
      } else { 
       throw new TooLongEx(); 
      } 
     } 
    } 
} 
+0

什麼失敗? TooLongEx定義在哪裏(或者你的意思是MessageTooLong? – MJB 2011-05-01 04:46:21

回答

2

似乎爲我工作的罰款。

對於

class TooLongEx extends Exception {} 

我得到(用於輸入長度> 20)

異常in thread「main」TooLongEx at MessageTooLong.main(MessageTooLong.java:26)