Reputation: 23
I have a program that accepts information from the user about a game object and then writes the array of game objects to a file. I also need to use an exception class I created. If the user inputs a document code other than Sp, St, or Gh, an exception needs to be thrown then caught and handled, allowing the user to try entering a proper code. I think my exception class is correct, but I don't really know how to implement it.
public class InvalidDocumentCodeException extends Exception //Here is my exception class
{
public InvalidDocumentCodeException (String message)
{
super (message);
}
}
import java.util.Scanner;
import java.io.*;
public class GamesList
{
public static void main (String[] args) throws InvalidDocumentCodeException, IOException
{
Scanner scan = new Scanner (System.in);
int count = 0, year;
String doMore = "y";
String file = "GameInfo.dat";
String documentCode, title, developer;
double price;
FileWriter fw = new FileWriter (file);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter outFile = new PrintWriter (bw);
//don't know if I need this
InvalidDocumentCodeException problem = new InvalidDocumentCodeException ("Incorrect document code!!!");
GameCollection games = new GameCollection();
while (doMore.equalsIgnoreCase("y")) //lets user input data until they are done
{
System.out.println("Enter a valid document code (Sp, St, Gh):");
documentCode = scan.nextLine();
try //my try catch
{
documentCode = "Sp";
}
catch(InvalidDocumentCodeException)
{
System.out.println("Oops! Try a valid code:");
documentCode = scan.nextLine();
}
System.out.println("Enter the game name:");
title = scan.nextLine();
System.out.println("Enter the game developer:");
developer = scan.nextLine();
System.out.println("Enter the games release year:");
year = scan.nextInt();
System.out.println("Enter the games price:");
price = scan.nextDouble();
games.addGame(documentCode, title, developer, year, price);
System.out.println();
System.out.println("This is great. Let's enter some more data. (Y/N):");
doMore = scan.next();
scan.nextLine();
}
outFile.println(games);
System.out.println("Data written to GameInfo.dat");
System.out.println(games);
outFile.close();
}
}
I've looked around, but maybe I'm just not understanding how exceptions and try catch statement work well enough.
Upvotes: 0
Views: 256
Reputation: 1577
When a method throws an exception the calling method must have a catch statement.
In this case, there is no method invoking the main method, so the the throw will give a stacktrace in stderr without you ever getting a chance to handle the exception.
If you want to do error checking in the main method use a try-catch block.
Upvotes: 1
Reputation: 8154
Usually you create and throw the exception when and where you need to. So, for instance, you could so something like:
try {
// Do something
if (somethingIsWrong) {
throw new MyException("This is broke, yo!");
}
} catch(MyException me) {
// Write code to handle the exception or log it...
}
The other way to do it is to call something that throws an exception:
try {
callToMethodThatThrowsSomeException(someVal);
} catch(SomeException se) {
// Handle the exception here...
}
Where callToMethodThatThrowsSomeException(someVal)
would look like:
public void callToMethodThatThrowsSomeException(String someVal) throws SomeException {
// Your code here, throwing a new SomeException for some reason...
}
In this case, SomeException
would extend Exception
. You could make it extend RuntimeException
instead, in which case you dont need to declare the throws on the method signature.
Upvotes: 1
Reputation: 272257
You're not throwing the exception e.g. your specification says
If the user inputs a document code other than Sp, St, or Gh
and that means you need code like(for example):
if (!isValidGameCode(documentCode)) {
throw new InvalidDocumentCodeException(documentCode);
}
i.e. you need to actually generate and throw the exception based on the input. At some stage above that in the method call hierarchy you need to handle it e.g. (in pseudocode)
while (!done) {
try {
getInput();
done = true;
}
catch (InvalidDocumentCodeException e) {
System.err.println("Not a valid code");
}
}
Upvotes: 1