-
Exception handling
Hey guys just working on a java homework again, it prompts the user twice, asking the user to enter a part number, and quantity, and both entry have to be int data type. I created a class that holds an array of error messages and called it DataMessages and it has a getMessage method. Code below:
Code:
public class DataMessages
{
String[] msgArray = new String[6];
public DataMessages()
{
msgArray[0] = "The part number is not numeric.";
msgArray[1] = "The quantity is not numeric.";
msgArray[2] = "The part number is too low(less than 0).";
msgArray[3] = "The part number is too high (more than 999).";
msgArray[4] = "The quantity ordered is too low (less than 1).";
msgArray[5] = "The quantity ordered is too high (more than 5,000).";
}
public String getMessage(int num)
{
return msgArray[num];
}
}
There is also a class I created to extend the exception class as shown below.
Code:
public class DataException extends Exception
{
DataMessages msg = new DataMessages();
public DataException(int num)
{
//Display message when the exception is caught
super("Error Code - " + num);
System.out.println(msg.getMessage(num));
}
}
So the error message will be displayed according to what the user enters. But here is my problem, error message 0 and 1 won't be able to display because java immediately does the inputmismatchexception and display only one of the error message regardless of which prompt they enter it for when the user enters a string. How can I make it so that when the user enter a string for the first prompt it will display error message 0 and display error message 1 for the 2nd prompt? Here is the actual class that test all of this:
Code:
// Xianyi Ye
// 3/11/2012
// Homework page 575 #10
import java.util.*;
public class PartAndQuantityEntry
{
public static void main(String[] args)throws DataException
{
Scanner scan = new Scanner(System.in);
int numP;
int numQ;
int terminal = 0;
int returnValue = terminal + 1;
//Terminates the program if the return value isn't 0
while(returnValue != terminal)
{
try
{
System.out.println("Enter 01 for both entry to quit\nPlease enter a part number: ");
numP = scan.nextInt();
System.out.println("Please enter quantity: ");
numQ = scan.nextInt();
returnValue = check(numP, numQ);
}
catch(DataException error)
{
System.out.println(error.getMessage());
}
catch(InputMismatchException error)
{
int num = 0;
throw (new DataException(num));
}
}
}
public static int check(int numP, int numQ) throws DataException
{
final int MAXP = 999;
final int MINP = 0;
final int MAXQ = 5000;
final int MINQ = 1;
final int QUIT = 01;
int returnVal = 1;
if(numP < MINP)
{
numP = 2;
throw (new DataException(numP));
}
if(numP > MAXP)
{
numP = 3;
throw (new DataException(numP));
}
if(numQ < MINQ)
{
numQ = 4;
throw (new DataException(numQ));
}
if(numQ > MAXQ)
{
numQ = 5;
throw (new DataException(numQ));
}
if(numP == QUIT && numQ ==QUIT)
returnVal = 0;
else
System.out.println("Valid entry");
return returnVal;
}
}
-
Re: Exception handling
-
Re: Exception handling
Yes all compiles and able to run it's just that when I try to get error message 0 and 1 I can't get it.
Like I try to enter a string for input for the first prompt but it only shows message 0 because i coded it like this:
Code:
catch(InputMismatchException error)
{
int num = 0;
throw (new DataException(num));
}
-
Re: Exception handling
When I first looked at it the code had an expression like "check(numP)" which was worth thinking about (and to which the compiler would have alerted you). Now the check() method is not used at all - leaving the catch(DataException) block doing nothing.
Personally I think the errant check(numP) might have pointed the way forward: since you are checking two values independently have two sets of try/catch blocks, and two different check methods each taking a single argument (the thing to be checked).
-
Re: Exception handling
thank you, I figured it out when you pointed out check(numP), which I previously minstakenly put but now I just made another check method to determine how to call the right error message thank you very much.
-
Re: Exception handling
-
Re: Exception handling
You've made something of a classic exception mistake here (and it's one that exists in several applications I've seen in use in businesses, so don't feel bad).
Those are all different exceptions (with the possible exception of the first two, though personally I'd still have them separate).
You might have them as part of a hierarchy, but they should be different classes.
This would solve most of your problem, to be honest.
The use of ids to differentiate exceptions is a mistake.