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 4 of 4

Thread: throwing Exception

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default throwing Exception

    Hi guys.
    I have two simple question for you, this is about programmer defined exception. (this source code come from thomas wu book, OOP introduction),

    firstly,
    is it okay for not write throws IllegalArgumentException in methode setPassword, as I know this methode throw exception?
    problem.jpg

    secondly,
    I have programmer defined Exception here, and in the AgeInputException Constactore there's no throws IllegalArgumentException.
    is it okay?
    ask.jpg
    Attached Images Attached Images


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: throwing Exception

    Please post the code here in the forum that you have questions about.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: throwing Exception

    haha, sorry Norm, I'm new here..

    Here there are

    I wonder In this defined Exception constructor, there's no throws IllegalArgumentException on the header of class,
    you can see that that constructor throwing an exception.
    /*
    Chapter 8 Sample Class: Customized Exception Class
    File: AgeInputException.java
    */
    class AgeInputException extends Exception {
    private static final String DEFAULT_MESSAGE = "Input out of bounds";
    private int lowerBound;
    private int upperBound;
    private int value;
    public AgeInputException(int low, int high, int input) {
    this(DEFAULT_MESSAGE, low, high, input);
    }
    public AgeInputException(String msg,  //<=== there's no throws IllegalArgument Exception here
    int low, int high, int input) {
    super(msg);
    if (low > high) {
    throw new IllegalArgumentException();
    }
    lowerBound = low;
    upperBound = high;
    value = input;
    }


    in this code too, in the setPassword methode there's no throws IllegalArgumentException too
    import java.io.*;
    class Resident implements Serializable {
    private String name;
    private String room;
    private String password;
    public Resident( ) {
    this("unassigned", "000", "XY12#$ab");
    }
    public Resident(String name, String room, String pwd)
    throws IllegalArgumentException {
    setName(name);
    setRoom(room);
    setPassword(pwd);
    }
    public String getName( ) {
    return name;
    }
    public String getPassword( ) {
    return password;
    }
    public String getRoom( ) {
    return room;
    }
    public void setName(String name) {
    this.name = name;
    }
    public void setPassword(String pwd) {
    int length = pwd.length();
    if (length < 8) {
    throw new IllegalArgumentException();
    } else {
    this.password = pwd;
    }
    }
    public void setRoom(String room) {
    this.room = room;
    }
    }

    so what do you think?

    thanks for your advice btw

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: throwing Exception

    Read the API doc for the exception and the class it extends.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Throwing Null Pointer Exception
    By SaurabhUpadhyay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 05:47 AM
  2. Quick question with throwing and catching an exception.
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2011, 10:24 PM
  3. Throwing an Exception in paintComponent
    By jmack in forum Exceptions
    Replies: 1
    Last Post: January 31st, 2011, 08:12 AM
  4. Problem with throwing an exception...
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 23rd, 2011, 12:06 AM
  5. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM