Java Exception printing twice -
I know there is no point in exception, but I was trying to figure out how to use exceptions. The only problem / problem caused by my exception exception is to hide the error message twice to the console.
Import java.io.File; Import java.io.phileNotFoundException; Import java.io.PrintStream; Import java.util.Scanner;
public square Project3 {public static zero main (string [] args) {try {string inputFileName = null; If (args.length & gt; 0) inputfile name = args [0]; File inputfile = FileGetter.getFile ("Enter the full path of the input file:", inputfile name); String output filename = null; If (args.length & gt; 1) output filename = Args [1]; File outputFile = FileGetter.getFile ("Enter the full path of the output file:", outputFileName); Scanner = new scanner (inputfile); Printstream out = new printstream (outputfile); Person person = null; // Read the record from the input file, get an object from the factory, // output the class in the output file. While (in.hasNext ()) {string personRecord = in.nextLine (); Person = PersonFactory.getPerson (individual); Person.display (); Person.output (outside); }} Hold (exception e) {System.err.println (e.getMessage ()); }}} Import java.util.Scanner; Students of the class will be given personal (GPA; Public student () {super (); GPA = 0.0; } Public student (string first name, string last name, double GPA) {super (first name, last name); This.gpa = GPA; } Public string toring () {try {if (gpa> gt; = 0.0 & amp; gpa & lt; = 4.0} {return super.toString ()) "+ \ n \ tGPA:" + gpa; } And {new invalidgapEXATION (); }} Hold (illegalgappaexception e) {System.out.println (e); Return super.toString () + "\ n \ tGPA:" + GPA; }} Public Zero Exposure () {System.out.println ("& lt; student;>";) ;; } @ Override public zero input (in scanner) {super.input (in); If (in.hasNextDouble ()) {this.gpa = in.nextDouble (); }} Class invalidGpaException expands exception {public invalid GpaException () {super ("illegal GPA:" + GPA); }}}
This is my console redout. I am not sure what is the reason for the exception of printing it twice.
project3.Student $ InvalidGpaException: Invalid GPA: -4.0 & lt; & Lt; Students & gt; & Gt; ID: 2Doo, Jr. GPA: -4.0 Project 3 Student $ InvalidGapException: Invalid GPA: -4.0
Edit: The main code is at the top of the input user specified file that I have shown here is my console printout, not in the output file Returns the output file shows the exact thing to zero with an error message. With the exception error message (which I know is not necessary) is printed on the console only I do not think Where I am printing it twice.
I estimate that your person.output ()
method calls To toString ()
, which will print the exception before returning the appropriate string, which is not shown because you are output it e: If you want your deduction, here is this: The first error Message and normal message are display ()
in the call, as it should be. Soon after, it is the output ()
call, by the name I think that display ()
does, leaving the file. However, you forgot that the exception is printed directly at System.out
, so it appears in the console, whereas the string which toString ()
is actually written on the file is .
Comments
Post a Comment