Results 1 to 17 of 17
Thread: Exception Handling
- 05-18-2011, 06:55 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Exception Handling
I'm trying to write a program that throws an exception if the input value is between 0 and 125 using exception errors. I'm not sure where to define the parameters.
import java.util.Scanner;
class InvalidAgeException extends Exception
{
public InvalidAgeException()
{
super("The Age you've entered is not valid");
}
}
class InputAge
{
public static void main(String args[])
{
try
{
Scanner inputdata = new Scanner( System.in );
System.out.print("Enter your name");
inputdata.nextInt();
System.out.print("Now enter your age");
int x = inputdata.nextInt();
}
catch(InvalidAgeException ae)
{
System.out.println("Invalid Age");
}
finally
{
System.out.println("Program Execution Completed");
}
}
}
- 05-18-2011, 08:02 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Where in your code are you going to throw an InvalidAgeException? I don't see it anywhere.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-18-2011, 08:02 PM #3
How to Throw Exceptions (The Java™ Tutorials > Essential Classes > Exceptions)
db
edit Now I know I'm getting old :(
- 05-18-2011, 09:37 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Why create an exception? Just have an if statement...
- 05-18-2011, 10:21 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, of you post, please wrap our code in code tags.
[code]
YOUR CODE HERE
[/code]
- 05-19-2011, 03:39 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
So now I've got the code in but it says "Exception in thread "main" java.lang.NoSuchMethodError: main"
Java Code:import java.util.Scanner; class InvalidAgeException extends Exception { public InvalidAgeException() { super("The Age you've entered is not valid"); } } class InputAge { public static void main(String args[]) { try { Scanner inputdata = new Scanner( System.in ); System.out.print("Enter your name"); inputdata.nextInt(); System.out.print("Now enter your age"); int x = inputdata.nextInt(); if (x < 0 && x > 125) { throw new InvalidAgeException(); } } catch(InvalidAgeException ae) { System.out.println("Invalid Age"); } finally { System.out.println("Program Execution Completed"); } } }
- 05-19-2011, 04:00 PM #7
Is your file called InputAge.java? Also, you need to make the InputAge class public.
- 05-19-2011, 04:30 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
now I got the program itself to work, but when i put in an age that is above 125, It doesn't throw an exception.
Java Code:import java.util.Scanner; public class InvalidAgeException extends Exception { public InvalidAgeException() { super("The Age you've entered is not valid"); } } class InputAge { public static void main(String args[]) { try { Scanner inputdata = new Scanner( System.in ); System.out.print("Enter your name"); String str1 = inputdata.nextLine(); System.out.print("Now enter your age"); int x = inputdata.nextInt(); if (x < 0 && x > 125) { throw new InvalidAgeException(); } } catch(InvalidAgeException ae) { System.out.println("Invalid Age"); } finally { System.out.println("Program Execution Completed"); } } }
- 05-19-2011, 04:36 PM #9
You're checking to see if the age is below 0 AND above 125. That'd be rather tricky.
- 05-19-2011, 04:50 PM #10
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
so do I have to create two separate exceptions?
- 05-19-2011, 04:57 PM #11
While that would work, I think it would be easier to check if the age is below 0 OR above 125.
- 05-19-2011, 05:03 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
O...Duh! I can be pretty retarded sometimes...
- 05-19-2011, 05:32 PM #13
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
nvrmnd. It still doesn't work
Java Code:import java.util.Scanner; public class InvalidAgeException extends Exception { public InvalidAgeException() { super("The Age you've entered is not valid"); } } class InputAge { public static void main(String args[]) { try { Scanner inputdata = new Scanner( System.in ); System.out.print("Enter your name"); String str1 = inputdata.nextLine(); System.out.print("Now enter your age"); int age = inputdata.nextInt(); if (age < 0 || age > 125) { throw InvalidAgeException(age); } } catch(InvalidAgeException ae) { System.out.println("Invalid Age"); } finally { System.out.println("Program Execution Completed"); } } }
- 05-19-2011, 05:35 PM #14
What, more precisely, doesn't work? It doesn't throw an exception when you enter 125 as the age?
- 05-19-2011, 05:56 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 05-19-2011, 05:57 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
darn, I replied twice; my keyboard mustn't be properly debounced ...
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-19-2011, 06:57 PM #17
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Exception Handling not working
By collwill in forum New To JavaReplies: 5Last Post: 05-02-2011, 05:37 AM -
Exception Handling
By eLancaster in forum New To JavaReplies: 4Last Post: 02-20-2011, 12:00 AM -
Exception Handling
By liljester in forum New To JavaReplies: 4Last Post: 06-21-2010, 03:09 PM -
Exception Handling help
By MZA in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:23 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks