使用catch块生成错误消息

时间:2016-04-21 16:55:31

标签: java exception return throw

如何获取NumberFormatException catch块以生成错误消息的第一行,以及从调用getMessage方法获取第二行

当输入文件输入无效数据时,该消息应该如下所示

****输入文件格式错误****

对于输入字符串:"非数字"

这是我的学术警告计划

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class AcademicWarning
{
  // -------------------------------------------------------------
  // Read student data (name, semester hours, quality points) from
  // a text file, compute the GPA, then write data to another file
  // if the student is placed on academic warning
  // -------------------------------------------------------------
  public static void main(String[] args)    
  {
    DecimalFormat two_digits = new DecimalFormat("0.00");
    Scanner            input = new Scanner(System.in);
    String input_file_name;
    String output_file_name;

    int    credit_hours;    // Number of semester hours earned
    double quality_points;  // Number of quality points earned
    double gpa;             // Grade point (quality point) average
    String student_name;

    System.out.print("Input file name: ");
    input_file_name = input.nextLine();
    System.out.print("Output file name: ");
    output_file_name = input.nextLine();

    try
    {
      Scanner      input_file = new Scanner(new File(input_file_name));
      PrintWriter output_file = new PrintWriter(new FileWriter(output_file_name));

      // ---------------------------------
      // Print a header to the output file
      // ---------------------------------
      output_file.println();
      output_file.println("Students on Academic Warning");
      output_file.println();

      // ------------------------------------------
      // Process the input file one token at a time
      // ------------------------------------------
      while (input_file.hasNext())
      {
        // ---------------------------------------
        // Get the credit hours and quality points
        // and if the student is on warning write
        // the student data to the output file
        // ---------------------------------------
        student_name   = input_file.next();
        credit_hours   = Integer.parseInt(input_file.next());
        quality_points = Double.parseDouble(input_file.next());
        gpa            = quality_points / credit_hours;
        if (gpa < 2.00)
          output_file.println(student_name + " " + credit_hours + " " +
                              two_digits.format(gpa));
      }
      output_file.close();
    }

    catch(NumberFormatException nfe)
    {
      System.out.println(nfe.getMessage());
    }

    catch(FileNotFoundException fnf)
    {
      System.out.println(fnf.getMessage());
    }

    catch(IOException ioe)
    {
      System.out.println(ioe.getMessage());
    }

  }
}

1 个答案:

答案 0 :(得分:0)

添加NUMERIC_ERROR_MESSAGE,例如以下示例:

public class AcademicWarning
{
  private static final String NUMERIC_ERROR_MESSAGE = "**** input file format error ****";

然后在您的catch块上:

catch(NumberFormatException nfe) {
  System.out.println(String.format("%s%n%s", NUMERIC_ERROR_MESSAGE, nfe.getMessage());
}

这将打印所需的错误标题+换行符(\ n)+异常消息。