Results 1 to 11 of 11

Thread: Exceptions

  1. #1
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default Exceptions

    Hey,
    I have been googling and seeing how to make custom exceptions but most are just misleading. Any help with example would be very much appreciated.
    Thanks.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    This tutorial is not misleading.

  3. #3
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default

    Thank you, but i do not see proper examples of custom exceptions there. It illustrates on theoretical part only.

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Custom exceptions are just subclasses of existing library exception classes - what did you have in mind?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have a related question of my own, if you don't mind, Dave: Do you ever make your Exception-derived classes public static inner classes?

  6. #6
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default

    yes theoretically i know custom exceptions are extended from existing library exception classes but i am new to it, so have been looking around but couldn't come up with something concise.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    I fail to see why you are fussing about all this, really. What exact information are you looking for? Have you tried to write an exception class and failed? If so post your attempt and the errors you got.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    800
    Rep Power
    6

    Default

    Please look into this code and ping me if you have any problems regarding the flow.It is understandable and a simple example.

    Java Code:
    import java.util.Scanner;
    
    //This customException class will be called if the age value is 0 or -1
    class CustomException extends Exception
    {
    	int age;
    
    	public String toString()
    	{
    		return "Your entered value "+ age +" for the Age is not valid ";
    	}
    
    	CustomException(int age)
    	{
    		this.age = age;
    	}
    
    }//CustomException
    
    class Test
    {
    	public static void main(String args[])
    	{
    		
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter the age ");
    		int age = sc.nextInt();
    		try
    		{
    			//Based on the below condition CustomException class object toString() method will
    			//be called automatically and printed to the user whenever customexceptin object is catched.
    
    			if((age == 0) ||(age < 0))
    			throw new CustomException(age);
    
    		}catch(CustomException e)
    		{
    			System.out.println(e);
    		}
    	}//main
    }//Test
    Ramya:cool:

  9. #9
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default

    i'm a beginner to java and have some issues regarding exceptions. I will try and figure it out from Ramya's example. Should i get any problem i shall post it. Thanks everyone.

  10. #10
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    This might help, it only allows a number between 10 and -10
    Java Code:
     public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     System.out.println("Enter a number");
     String SmallInteger="";
     SmallInteger = scan.nextLine();
     Integer num = new Integer(SmallInteger);
            try {
                if (num > 10 || num < -10) {
                    throw new Exception();
                }
            }
            catch (Exception PleaseCatchItForMe) {
                System.out.println ("Put in a number between -10 and 10");
            }
        }

  11. #11
    hedonist is offline Member
    Join Date
    Jun 2009
    Posts
    35
    Rep Power
    0

    Default

    Thanks Ramaya, things are a lot clearer now. Thanks jigglywiggly for a new perspective.

Similar Threads

  1. Exceptions in thread
    By Goodwater in forum New To Java
    Replies: 4
    Last Post: 03-16-2009, 03:00 AM
  2. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 07:12 PM
  3. How to use chained exceptions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 02:50 PM
  4. Better way to handle exceptions
    By javaplus in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2008, 06:47 PM
  5. Project of Exceptions
    By Albert in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 03:09 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
  •