簡體   English   中英

Java異常打印兩次

[英]Java Exception printing twice

我知道異常是沒有意義的,但我試圖學習如何使用/創建異常,所以我使用了它。 唯一的問題是由於某種原因我的異常生成的錯誤消息是打印到控制台兩次。

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner;

public class Project3
{

  public static void main(String[] args)
  {
    try
    {
      String inputFileName = null;
      if (args.length > 0)
        inputFileName = args[0];
      File inputFile = FileGetter.getFile(
          "Please enter the full path of the input file: ", inputFileName);

      String outputFileName = null;
      if (args.length > 1)
        outputFileName = args[1];
      File outputFile = FileGetter.getFile(
          "Please enter the full path of the output file: ", outputFileName);

      Scanner in = new Scanner(inputFile);
      PrintStream out = new PrintStream(outputFile);
      Person person = null;

      // Read records from input file, get an object from the factory,
      // output the class to the output file.
      while(in.hasNext())
      {
        String personRecord = in.nextLine();

        person = PersonFactory.getPerson(personRecord);

        person.display(); 

        person.output(out);
      }
    } catch (Exception e)
    {
      System.err.println(e.getMessage());
    }
  }

}





import java.util.Scanner;

class Student extends Person
{
  private double gpa;

  public Student()
  {
    super();
    gpa = 0.0;
  }

  public Student(String firstName, String lastName, double gpa)
  {
    super(firstName, lastName);
    this.gpa = gpa;
  }

  public String toString(){
   try{
        if (gpa >= 0.0 && gpa <= 4.0){
            return super.toString() + "\n\tGPA: " + gpa;
        }
        else {
            throw new InvalidGpaException();
        }
    }
   catch (InvalidGpaException e){
       System.out.println(e);
       return super.toString() + "\n\tGPA: " + gpa;
   }
  }

  public void display()
  {
    System.out.println("<<Student>>" + this);
  }

  @Override
  public void input(Scanner in)
  {
    super.input(in);

    if (in.hasNextDouble())
    {
      this.gpa = in.nextDouble();
    }
  }

  class InvalidGpaException extends Exception {
    public InvalidGpaException() {
        super("Invalid GPA: " + gpa);
      }
  }
}

這是我的控制台讀數。 不確定是什么導致異常打印兩次。

project3.Student$InvalidGpaException: Invalid GPA: -4.0
<< Student>>
        Id: 2        Doe, Junior
        GPA: -4.0
project3.Student$InvalidGpaException: Invalid GPA: -4.0

編輯:主要代碼在頂部。 輸入是用戶指定的文件。 我在這里顯示的是我的控制台打印輸出,而不是返回到輸出文件的內容。 輸出文件顯示完全相同的內容減去錯誤消息。 來自異常的錯誤消息(我知道不是必需的)僅打印到控制台。 我沒看到我在哪里打印兩次。

我的猜測是你的Person.output()方法在其中調用了toString() ,它會在返回正確的字符串之前打印異常,因為你輸出它而沒有顯示out

E:如果你想要我的演繹,這里是:第一條錯誤信息和正常信息在show display()的調用中打印出來,應該如此。 緊接着就是output()調用,我猜這個名稱是為了做display()所做的,除了文件。 但是,您忘記了異常直接打印到System.out ,因此它出現在控制台中,而toString()實際返回的字符串將寫入該文件。

你主要是什么? 什么是你的INPUT ..將你的例外改為不同的東西。

你在哪里打印這些數據?

<< Student>>
        Id: 2        Doe, Junior
        GPA: -4.0

你確定你沒有兩次調用person.toString()嗎?

我的猜測是你在你未向我們展示的代碼中的某個地方調用了toString

放入Thread.dumpStack(); toString實現中應該告訴你從哪里來。

嘗試改變這個:

System.out.println(e);
   return super.toString() + "\n\tGPA: " + gpa;

System.out.println(e);

(或類似的東西)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM