Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Quick question with throwing and catching an exception.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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"

    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);
        }
    }
    public class IdNumberException extends Exception
    {
        public IdNumberException()
        {
        super("Invalid ID Number");
        }
        public IdNumberException(String message)
        {
        super(message);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Quick question with throwing and catching an exception.

    Where is the try statement that should be before the catch?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Quick question with throwing and catching an exception.

    Add a try statement well before the throw statement to pair with the catch.

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default 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
    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)
    try{
       Student s = new Student(...);
    }catch(IdNumberException e){
        e.printStackTrace();
    }
    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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).
    public Student(int id, String stName) throws IdNumberException

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

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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:

        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!

Similar Threads

  1. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM
  2. Throwing an Exception in paintComponent
    By jmack in forum Exceptions
    Replies: 1
    Last Post: January 31st, 2011, 07:12 AM
  3. Problem with throwing an exception...
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 22nd, 2011, 11:06 PM
  4. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 09:43 PM
  5. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 04:48 PM