Java异常处理?

问题描述:

以下代码计算存储在文本文件中的数字的平均值。 我添加了一些“文件未找到”错误的异常处理。我需要知道如何在文本文件中的数据不是数字(或不是int)时添加另一个异常。我想增加多个捕获。不知道如何?Java异常处理?

import java.util.*; 
import java.io.*; 

public class NumAvg2 { 

    public static void main(String[] args) 
    { 
    int c = 1; 
    long sum = 0; 
    String strFileName; 

    strFileName = args[0]; 

    Scanner scFileData; 
    try{ 
     scFileData = new Scanner (new File(strFileName)); 

     while (scFileData.hasNext()) 
     { 
      sum = sum + scFileData.nextInt(); 
      c++; 
     } 

    scFileData.close(); 
    System.out.println("Number of integers: " + (c - 1)); 
    System.out.println("Average = " + (float)sum/(c - 1)); 

    } catch (FileNotFoundException e) { 
     System.out.println("File not found!"); 
     System.exit(1); 
    } 

    } 
} 
+0

您可以读取一个字符串并尝试解析。没有很好的解析能力发现错误 – elyashiv

注意Scanner.nextInt也会引发InputMismatchException如果下一个标记是不是整数。

docs

抛出: InputMismatchException - 如果下一个标记不匹配整数的正则表达式,或超出范围

InputMismatchExceptionRuntimeException,所以能在try/catch区块中被忽略,但您也可以明确处理它:

try 
{ 
    // ... Scanner.nextInt 
} 
catch (FileNotFoundException e) 
{ 
    System.out.println("File not found!"); 
    System.exit(1); 
} 
catch (InputMismatchException e) 
{ 
    // ... not an int 
} 

你在哪里将你的数据从文件转换为整数?您应该添加一个的try-catch有..

而不是使用下面的while循环,在那里你必须赶上例外TypeMismatch的: -

while (scFileData.hasNext()) { 
     try { 
      sum = sum + scFileData.nextInt(); 
      c++; 
     } catch (InputMismatchException e) { 
      System.out.println("Invalid Input"); 
     } 
    } 

您可以使用类似下面的变化: -

while (scFileData.hasNextInt()) 
    { 
     sum = sum + scFileData.nextInt(); 
     c++; 
    } 

scanner.hasNextInt()会自动检查是否为整数或不输入..

而且,您声明外侧y您的变量我们的try-catch块..我建议不要使用一个大的try-catch块..

相反,你可以围绕一个try-catch语句围绕file reading(这将是一个语句)。然后在几个语句,您可以再次环绕你左右scanner.nextInt()其他的try-catch ..

所以,我会修改你这样的代码: -

public class NumAvg2 { 

public static void main(String[] args) 
{ 
    int c = 1; 
    long sum = 0; 
    String strFileName; 

    strFileName = args[0]; 

    Scanner scFileData; 

    try{ 
     scFileData = new Scanner (new File(strFileName)); 

    } catch (FileNotFoundException e) { 
     System.out.println("File not found!"); 
     System.exit(1); 
    } 
    while (true) { 
     if (scFileData.hasNextInt()) 
     { 
      sum = sum + scFileData.nextInt(); 
      c++; 
      break; 
     } else { 
      System.out.println("Enter an integr"); 
     } 
    } 

    scFileData.close(); 
    System.out.println("Number of integers: " + (c - 1)); 
    System.out.println("Average = " + (float)sum/(c - 1)); 

} 
} 

这样,你的代码变得更加清洁.. *只是一个建议..

+0

好的。我也使用这种方法工作。在循环中添加了第二个try/catch,但是您还必须将System.exit(1); –

+0

你现在不需要..看到我用'scanner.hasNextInt()'在同时。它会自动处理它。它只会在里面,而如果去数是整数.. –

+0

见修改后的代码..我现在使用了一个'if' ..如果输入不是整数,则不需要执行'System.exit(1)'。只需要输入,直到给出整数。 –

为了赶上多个异常,你把它们连像这样:

public class NumAvg2 { 

public static void main(String[] args) 
{ 
int c = 1; 
long sum = 0; 
String strFileName; 

strFileName = args[0]; 

Scanner scFileData; 
try{ 
    scFileData = new Scanner (new File(strFileName)); 

    while (scFileData.hasNext()) 
    { 
     sum = sum + scFileData.nextInt(); 
     c++; 
    } 

scFileData.close(); 
System.out.println("Number of integers: " + (c - 1)); 
System.out.println("Average = " + (float)sum/(c - 1)); 

} catch (FileNotFoundException e) { 
    System.out.println("File not found!"); 
    System.exit(1); 
} catch(InputMismatchException e){ 
    //handle it 
} 
} 
} 

... 
} catch (FileNotFoundException e) { 
    System.out.println("File not found!"); 
    System.exit(1); 
} catch (InputMismatchException e) { 
    // Add stuff into your new exception handling block 
} 
... 

您的IDE可能并没有抱怨,因为这是InputMismatchException时一个RuntimeException(未经检查的异常),而FileNotFoundException异常是一个普通的检查异常。

+0

}赶上(InputMismatchException时五){ \t //添加的东西到新的异常处理块 } –

} catch (FileNotFoundException | InputMismatchException e) { 
    e.printStackTrace(); 
    System.exit(1); 
} 

如果你想

try { 
... 
} 
catch (FileNotFoundException|InputMismatchException ex) { 
//deal with exception 
} 

这比多渔获清洁了一下,您可以使用Java 7的multi catch。如果抛出这些异常中的任何一个,它将被捕获到单个catch块中

+0

这工作:}赶上(FileNotFoundException异常| InputMismatchException时五){ 的System.out.println( “!出错”); System.out.println(e.getMessage()); System.exit(1); } –

+0

这是相当... + 1的新功能 –

+0

@FAUZIRASHID当你捕获异常,从来只打印您自己的消息。总是打印出例外情况。否则,你无法知道实际的错误是什么。 – EJP

} catch (InputMismatchException e) { 
    System.out.println("Not integer!"); 
    System.exit(1); 
} catch (FileNotFoundException e) { 
    System.out.println("File not found!"); 
    System.exit(1); 
}