2015-12-08 50 views
2

我在我制作的控制台程序中出错,其中我似乎无法处理错误。Java错误处理 - 这是做这件事的最好方法吗?

如果用户输入了不正确的输入类型(如其中一个int去的浮动),他们被训斥和程序功能(即使该程序关闭时,它做它方式):

while(!scanner.hasNextLong()){ 
       System.out.println("You entered something bad.. Why do you hurt me?"); 
       System.out.println("*Calcumatastic dies, and I hope you feel remorseful*"); 
       try { 
        Thread.sleep(4000); 
       } catch (InterruptedException f) { 
        f.printStackTrace(); 
       } 
       System.exit(0); 
      } 

如果用户输入,例如,一个零作为一个变量,它不能等于零(由于零分裂),用户被训斥,和生命仍在继续,并且程序仍然功能:

while (b == 0) { 
      System.out.println("Did you even read the instructions?? You cannot divide by zero!"); 
      System.out.println(); 
      System.out.println("Second Integer: "); 
      b = scanner.nextLong(); 
     } 

但是,如果用户att空格除以零,则输入一个不正确的输入类型,程序崩溃。我究竟做错了什么?我试图进入一个try/catch- while循环,因为我在其他情况下没有,但它似乎并没有这样的伎俩:

System.out.println("Second Integer: "); 
     while(!scanner.hasNextLong()){ 
      System.out.println("You entered something bad.. Why do you hurt me?"); 
      System.out.println("*Calcumatastic dies, and I hope you feel remorseful*"); 
      try { 
       Thread.sleep(4000); 
      } catch (InterruptedException f) { 
       f.printStackTrace(); 
      } 
      System.exit(0); 
     } 
     b = scanner.nextLong(); 
     while (b == 0) { 
      System.out.println("Did you even read the instructions?? You cannot divide by zero!"); 
      System.out.println(); 
      System.out.println("Second Integer: "); 
      b = scanner.nextLong(); 
     } 
+1

你是什么意思,'崩溃'?你会得到一个异常和堆栈跟踪? – Kenney

回答

0

如果试图通过零执行除法,一个java.lang.ArithmeticException是抛出,当你试图阅读一个长时间和用户输入一些无法解析的东西时,抛出InputMismatchException(可能是你称之为“崩溃”)。

由于您在使用b = scanner.nextLong();时发生错误,因此当用户输入0而不检查用户是否输入了有效的长整型值时。

这里是你的代码调整:

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 

    long b = 0; 

    do { 
     System.out.println("Second Integer: "); 

     b = readLong(scanner); 

     if (b == 0) { 
      System.out.println("Did you even read the instructions?? You cannot divide by zero!"); 
      System.out.println(); 
     } 
    } while (b == 0); 
} 

private static long readLong(Scanner scanner) { 
    if (!scanner.hasNextLong()) { 
     System.out.println("You entered something bad.. Why do you hurt me?"); 
     System.out.println("*Calcumatastic dies, and I hope you feel remorseful*"); 
     try { 
      Thread.sleep(4000); 
     } catch (InterruptedException f) { 
      f.printStackTrace(); 
     } 
     System.exit(0); 
    } 
    return scanner.nextLong(); 
} 
+0

正确,InputMismatchException被抛出,它不是崩溃。对不起,术语错误,我有点小菜鸟。谢谢您的帮助! – MStrain

+0

没问题。我们在这里提供帮助。不要忘记提高或降低答案。 – andrucz

0

你的问题是,您输入验证似乎是连续的。也就是说,你检查无效输入,然后检查零值。每次有输入时,你可能都想要这样做。

您可能想检查单个循环中的所有错误情况。例如:

System.out.println("Second Integer: "); 
// a loop wrapping your other validation checks 
while(scanner.hasNext()){ // while there is input: 
    while(!scanner.hasNextLong()){ // check for non-long input 
     System.out.println("You entered something bad.. Why do you hurt me?"); 
     System.out.println("*Calcumatastic dies, and I hope you feel remorseful*"); 
     try { 
      Thread.sleep(4000); 
     } catch (InterruptedException f) { 
      f.printStackTrace(); 
     } 
     System.exit(0); 
    } 
    b = scanner.nextLong(); 
    while (b == 0) { // check for non-zero input 
     System.out.println("Did you even read the instructions?? You cannot divide by zero!"); 
     System.out.println(); 
     System.out.println("Second Integer: "); 
     b = scanner.nextLong(); 
    } 
} 
相关问题