Results 1 to 2 of 2
- 11-16-2012, 05:23 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
Creating a personal exception class.
We're supposed to create a program for a class wherein it accepts an input for an age, then if the age is less than 0 throws an exception and stops the program. I cannot figure out how exception classes work for the life of me. Any help on where I'm going wrong? Thanks!
Exception:Java Code:import java.util.Scanner; public class A { public static void checkAge(int age) throws Exception{ if(age < 0){ ageException b = new ageException("Invalid."); } } public static void main(String[] args) throws Exception { A a = new A(); Scanner keyboard = new Scanner(System.in); System.out.print("Enter your age: "); int age = 0; try{ age = keyboard.nextInt(); checkAge(age); } catch (ageException b) { System.out.println(b.v); } System.out.println("Thanks. You say you are "+ age + " years old."); } }
Java Code:public class ageException extends Exception { String v; public ageException(){ } ageException(String z){ String v = z; } public static void main(String[] args) { } }
- 11-16-2012, 05:52 AM #2
Re: Creating a personal exception class.
Your overloaded constructor on line 8 is missing the the scope - it should also be public. In your checkAge method, you need to throw a new exception:
Also, this is just a style note (but an important one), your class names should never start with a lowercase letter - lowercase is reserved for methods, variables, and key words.Java Code:throw new ageException("My message here");
Similar Threads
-
SQL Exception when creating table
By wdh321 in forum JDBCReplies: 15Last Post: 10-23-2012, 06:31 PM -
Creating and implementing class for creating a calendar object
By kumalh in forum New To JavaReplies: 9Last Post: 07-29-2011, 02:18 PM -
Creating Exception class
By Bgreen7887 in forum New To JavaReplies: 1Last Post: 12-09-2010, 08:18 PM -
Creating your own exception
By joms999 in forum New To JavaReplies: 2Last Post: 02-17-2010, 04:33 PM -
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks