Java异常打印两次 [英] Java Exception printing twice

查看:205
本文介绍了Java异常打印两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个异常是无意义的,但我试图学习如何使用/创建异常,所以我用这个。唯一的问题是由于某种原因我的异常生成的错误消息是打印到控制台两次。

I know the exception is kind of pointless, but I was trying to learn how to use / create exceptions so I used this. The only problem is for some reason my error message generated by my exception is printing to console twice.

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

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);
      }
  }
}

不确定导致异常打印两次的原因。

This is my console readout. Not sure what's causing the exception to print twice.

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

编辑:主代码在顶部。输入是用户指定的文件。我在这里显示的是我的控制台打印输出,而不是返回到输出文件。输出文件显示完全相同的东西减去错误消息。来自异常的错误消息(我知道没有必要)只打印到控制台。我不知道我在哪里打印它两次。

edit: The main code is on the top. The input is a file designated by the user. What I shown right here is my console printout, not what is returned to the output file. The output file shows the exact same thing minus the error message. The Error message from the exception (which I know is not necessary) is only printed to the console. I don't see where I'm printing it twice.

推荐答案

我猜你的 .output()方法在它中调用 toString(),它会在返回正确的字符串之前打印异常,因为你输出到 out

My guess is that your Person.output() method has a call to toString() in it, which will print the exception before returning the proper string, which doesn't show up because you're outputting it to out.

E:如果你想要我的扣除, :第一个错误消息和正常消息在调用 display()中打印出来,因为它应该是。紧接着是 output()调用,其名称我想是做什么 display()做,除了到一个文件。但是,您忘记了异常直接打印到 System.out ,所以它出现在控制台中,而 toString()实际返回的是写入文件。

E: If you want my deduction, here it is: The first error message and normal message are printed out within the call to display(), as it should be. Immediately after that is the output() call, which by the name I guess is meant to do what display() does, except to a file. However, you forgot that the exception is printed directly to System.out, so it appears in the console, while the string that toString() actually returns is written to the file.

这篇关于Java异常打印两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆