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: Exception handling

  1. #1
    Junior Member
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception handling

    hi all,i have written a code which shows exception when the age is less than 18 or greater than 22,its working perfectly.But now i want to enter the age during runtime,will someone change the code and give me.Thank u!

    class userException extends Exception
    {
    int age;
     
    userException(int age)
    {
    age=age;
    }
     
    public String toString()
    {
    return"UserException caught:invalid age";
    }}
     
     
    class userExceptionDemo
    {
    static void calculate(int age) throws userException
    {
     
    if(age<18||age>22)
    throw new userException(age);
    System.out.println("valid age");
    }
     
    public static void main(String args[])
    {
    try
    {
     
    calculate(20);
    calculate(34);
     
    }
    catch(userException obja)
    {
    System.out.println("caught:"+obja);
    }
    }
    }
    Last edited by JavaPF; August 12th, 2008 at 01:17 PM. Reason: added [code] [/code] tags


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Exception handling

    Hello AnithaBabu1.

    Welcome to the Java Programming Forums!

    Try this:

    class userException extends Exception
    {
    int age;
     
    userException(int age)
    {
    age=age;
    }
     
    public String toString()
    {
    return"UserException caught:invalid age";
    }}
     
     
    class userExceptionDemo
    {
    static void calculate(int age) throws userException
    {
     
    if(age<18||age>22)
    throw new userException(age);
    System.out.println("valid age");
    }
     
    public static void main(String args[])
    {
     
    System.out.println("Enter age: ");
     
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    int age = Integer.parseInt(s);
     
    try
    {
     
    calculate(age);
    calculate(age);
     
    }
    catch(userException obja)
    {
    System.out.println("caught:"+obja);
    }
    }
    }

    This code takes input from the console.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception handling

    Thank u!but it's showing certain error like can't find symbol for those streams.I tried to do certain changes but i didn't get the o/p.I am new to java.

  4. #4
    Junior Member
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception handling

    Sorry,i didn't import io exception package thats y i got the error,but now iam getting different error at br.readLine() method showing "unreported exception java.io.IOException:must be caught or declared to be thrown",will u plz point out whatz the problem is?I have highlighted the particular line which shows error with red colour.Thank u!

    class userException extends Exception
    {
    int age;
     
    userException(int age)
    {
    age=age;
    }
     
    public String toString()
    {
    return"UserException caught:invalid age";
    }}
     
     
    class userExceptionDemo
    {
    static void calculate(int age) throws userException
    {
     
    if(age<18||age>22)
    throw new userException(age);
    System.out.println("valid age");
    }
     
    public static void main(String args[])
    {
     
    System.out.println("Enter age: ");
     
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    [COLOR="Red"]String s = br.readLine();[/COLOR]
    int age = Integer.parseInt(s);
     
    try
    {
     
    calculate(age);
    calculate(age);
     
    }
    catch(userException obja)
    {
    System.out.println("caught:"+obja);
    }
    }
    }

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Exception handling

    Try putting this part of the code into a try catch block..

    System.out.println("Enter age: ");
     
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    [COLOR=red]String s = br.readLine();[/COLOR]
    int age = Integer.parseInt(s);
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Junior Member
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception handling

    Thank a lot PF,it works.I tried to mark it solved,in my original post using edit button,but i didn't see any drop down box with the label solved near my title!any have thank u so much..........!!!!

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Cool Re: Exception handling

    Quote Originally Posted by AnithaBabu1 View Post
    Thank a lot PF,it works.I tried to mark it solved,in my original post using edit button,but i didn't see any drop down box with the label solved near my title!any have thank u so much..........!!!!
    Thats no problem. Glad I could help..

    Yeah there seems to be a problem with people marking threads as solved. I'm looking into it now.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  3. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM