Quick question with throwing and catching an exception.
Hey guys, I'd first just like to thank in advance for anyone taking the time to help out. Basically, I'm working on a project, and what I have to do is make a class that descends from exception, and make another class that throws that particular exception. In the constructor, I'm supposed to check to make sure that the idNumber is valid, and if not I'm supposed to throw the exception. If anyone can help out or give me a few tips that'd be great! Thanks again
P.S the error I'm getting with what I currently have is on the line that says "catch(IdNumberException)" and it is saying "illegal start of type ';' expected"
Code :
public abstract class Student
{
private String name;
private int idNumber;
public Student(int id, String stName)
{
name = stName;
idNumber = id;
if(idNumber < 1 && idNumber > 9999)
throw new IdNumberException();
}
catch(IdNumberException e)
{
System.out.println(e.getMessage());
}
public String toString()
{
return("Name: " + name + " ID Number: " + idNumber);
}
}
Code :
public class IdNumberException extends Exception
{
public IdNumberException()
{
super("Invalid ID Number");
}
public IdNumberException(String message)
{
super(message);
}
}
Re: Quick question with throwing and catching an exception.
Where is the try statement that should be before the catch?
Re: Quick question with throwing and catching an exception.
The catch statement was there for the throw statement. I guess that's my main question, I understand how try...catch works, but im not 100% on how the throw works. I assumed that since there was an error with the throw new IdNumberException, that it was expecting a catch statement to catch it.
Re: Quick question with throwing and catching an exception.
Add a try statement well before the throw statement to pair with the catch.
Re: Quick question with throwing and catching an exception.
Its not just a missing try, the catch statement is outside any context to be meaningful. If you throw an exception (unchecked), you must either catch it or define the method that contains the throw statement to throw said exception...in your code posted, you could define the constructor to throw your exception
Code :
public Student(int id, String stName) throws IdNumberException
In which case, anything constructing a Student class must try/catch the constructor (assuming IdNumberException is not unchecked)
Code :
try{
Student s = new Student(...);
}catch(IdNumberException e){
e.printStackTrace();
}
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Re: Quick question with throwing and catching an exception.
Thanks much, I'm just confused with how it knows to throw the IdNumberException only if the if statement is true. For instance, if I have the following code, how does it know to throw an IdNumberException? Ill display a second box of code too. It is my class where I actually instantiate a student. So you can see how that is setup. (The main method is provided for the problem, one of the instructions of the problem is to check the id number in the student constructor, and if it's invalid (not between 1-9999) throw an IdNumberException).
Code :
public Student(int id, String stName) throws IdNumberException
Code :
public class TestStudent
{
public static void main(String[] args)
{
try
{
Student s1 = new GradStudent(101, "Smith", "BSCS");
System.out.println(s1);
Student s2 = new PhdStudent(99999, "Jones", "BSEE", "MSEE");
System.out.println(s2);
}
catch(IdNumberException ide)
{
System.out.println(ide.getMessage());
}
}
}
Re: Quick question with throwing and catching an exception.
eh I got it guys, it basically boils down to me being retarded. In order to throw an exception like that, I have to declare it at the beginning of the constructor that I'm going to throw and exception. The final code for the constructor looks like this:
Code :
public Student(int id, String stName) throws IdNumberException
{
if(idNumber < 1 && idNumber > 9999)
throw new IdNumberException();
name = stName;
idNumber = id;
}
Thanks again to everyone who took their time to help out and provide any advise. It really is greatly appreciated!