|
|||
|
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 |
|
|||
|
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);
}
}
}
|
|
||||
|
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);
|
|
||||
|
Quote:
Yeah there seems to be a problem with people marking threads as solved. I'm looking into it now. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
|
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 |