Register FAQ Calendar Search Today's Posts Mark Forums Read

Go Back   Java Programming Forums > Java Programming > New to Java

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-08-2008, 01:01 PM
Junior Member
 
Join Date: Aug 2008
Posts: 8
AnithaBabu1 is on a distinguished road
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!

Java Code:
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; 12-08-2008 at 06:17 PM. Reason: added [code] [/code] tags
Reply With Quote
  #2 (permalink)  
Old 13-08-2008, 01:44 PM
JavaPF's Avatar
Java Guru
 
Join Date: May 2008
Posts: 147
JavaPF will become famous soon enoughJavaPF will become famous soon enough
Default Re: Exception handling

Hello AnithaBabu1.

Welcome to the Java Programming Forums!

Try this:

Java Code:
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.
Reply With Quote
  #3 (permalink)  
Old 16-08-2008, 04:32 AM
Junior Member
 
Join Date: Aug 2008
Posts: 8
AnithaBabu1 is on a distinguished road
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.
Reply With Quote
  #4 (permalink)  
Old 16-08-2008, 06:02 AM
Junior Member
 
Join Date: Aug 2008
Posts: 8
AnithaBabu1 is on a distinguished road
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!

Java Code:
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);
}
}
}
Reply With Quote
  #5 (permalink)  
Old 18-08-2008, 02:26 PM
JavaPF's Avatar
Java Guru
 
Join Date: May 2008
Posts: 147
JavaPF will become famous soon enoughJavaPF will become famous soon enough
Default Re: Exception handling

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

Java Code:
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);
Reply With Quote
  #6 (permalink)  
Old 26-08-2008, 06:01 AM
Junior Member
 
Join Date: Aug 2008
Posts: 8
AnithaBabu1 is on a distinguished road
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..........!!!!
Reply With Quote
  #7 (permalink)  
Old 27-08-2008, 02:37 PM
JavaPF's Avatar
Java Guru
 
Join Date: May 2008
Posts: 147
JavaPF will become famous soon enoughJavaPF will become famous soon enough
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Custom Search
100 most searched terms
Search Cloud
"bad endpoint type" bad endpoint type com.sun.xml.internal.messaging.saaj.soapexceptionimpl com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad endpoint type com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed convert arraylist to map date_format_now eclipse short keys eclipse shortcut keys ejb3 quartz image character recognition java java convert double to binary java forum java forums java jtextarea font java jtextarea font size java programing forums java programmer forum java programmers forum java programming forum java programming forums java read last line java read last line of file java sendkeys java.security.privilegedactionexception java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad response java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed jtextarea font jtextarea font size last line file java programing forums programming forums reset jcombobox saaj0008 saaj0008 "bad response bad request" saaj0008: bad response; bad request saaj0008: bad response; not found sendkeys java severe: saaj0008 severe: saaj0008: bad response; bad request severe: saaj0008: bad response; not found soapexceptionimpl: bad endpoint type soapexceptionimpl: java.security.privilegedactionexception textpad java

All times are GMT. The time now is 01:10 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.