Results 1 to 2 of 2
  1. #1
    Zoralink23 is offline Member
    Join Date
    Sep 2012
    Rep Power

    Default 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!

    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.out.print("Enter your age: ");
    		int age = 0;
    			age = keyboard.nextInt();
    		catch (ageException b) {
    			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) {

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power

    Default 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:

    Java Code:
    throw new ageException("My message here");
    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.

Similar Threads

  1. SQL Exception when creating table
    By wdh321 in forum JDBC
    Replies: 15
    Last Post: 10-23-2012, 06:31 PM
  2. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  3. Creating Exception class
    By Bgreen7887 in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 08:18 PM
  4. Creating your own exception
    By joms999 in forum New To Java
    Replies: 2
    Last Post: 02-17-2010, 04:33 PM
  5. Replies: 1
    Last Post: 01-03-2010, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts