Java 如何知道文件的哪一行发生了IOexception

Java 如何知道文件的哪一行发生了IOexception,java,exception,Java,Exception,在我的程序中,我想从文本文件中读取。我的文件中的数据是以空格分隔的数字。我将在程序中将它们转换为整数。如果从文件读取的数据不是数字,我想捕获异常。我想知道错误发生在文件的哪一行(数据不是数字)。我知道我应该捕获NumberFormatException异常,但不知道如何找到异常的行。 下面是一些代码: try { //...using Bufferreader ...some code here while ((strLine = br.readLine()) != null )

在我的程序中,我想从文本文件中读取。我的文件中的数据是以空格分隔的数字。我将在程序中将它们转换为整数。如果从文件读取的数据不是数字,我想捕获异常。我想知道错误发生在文件的哪一行(数据不是数字)。我知道我应该捕获NumberFormatException异常,但不知道如何找到异常的行。 下面是一些代码:

try 
{ 
  //...using Bufferreader ...some code here
  while ((strLine = br.readLine()) != null ) 
  {
    String[] term = strLine.split(" ");  
    //for now i just work with the first element of the array
    int num = Integer.parseInt(term[0]); 
    //....some code here
  }
  in.close();
}
catch (NumberFormatException eg)
{
  System.out.println("Error: there is a problem in line ..? " );
}
当参数不是数字时,上述行将抛出NumberFormatException

int line = 0;
try 
{ 
  //...using Bufferreader ...some code here
  while ((strLine = br.readLine()) != null ) 
  {
    line ++;
    String[] term = strLine.split(" ");  
    //for now i just work with the first element of the array
    int num = Integer.parseInt(term[0]); 
    //....some code here
  }
  in.close();
}
catch (NumberFormatException eg)
{
  System.out.println("Error: there is a problem in line " + line );
}
当参数不是数字时,上述行将抛出NumberFormatException

int line = 0;
try 
{ 
  //...using Bufferreader ...some code here
  while ((strLine = br.readLine()) != null ) 
  {
    line ++;
    String[] term = strLine.split(" ");  
    //for now i just work with the first element of the array
    int num = Integer.parseInt(term[0]); 
    //....some code here
  }
  in.close();
}
catch (NumberFormatException eg)
{
  System.out.println("Error: there is a problem in line " + line );
}