Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



What are user defined exceptions in Java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 30-11-2009
Member
 
Join Date: Nov 2009
Posts: 51
What are user defined exceptions in Java?
  

Hi,
I am last year computer science student. In our last java paper there is one question asked What are user defined exceptions in Java? I unable to understand what is it. So if you know what is mean by "user defined exceptions" please help me.

Reply With Quote
  #2  
Old 30-11-2009
Member
 
Join Date: May 2008
Posts: 2,289
Re: What are user defined exceptions in Java?

Though Java language provides it's in-built exceptions there are some situation where we need to create our own exceptions to handle particular error.
We must use following rule to create user defined exceptions:
1.User defined exception must be extend from particular Exception class.
2.The toString() method must be used in user defined exception.

Now used following code to understand user defined exceptions :

Code:
public class userdefinedException {

    public static void main(String[] args) throws Exception{

        int a = getAge1();

        if (a < 0){
            throw new NegativeAgeException(a);
        }else{
            System.out.println("Age get is " + a);
        }
    }
    
    static int getAge1(){
        return -10;
    }
}
Reply With Quote
  #3  
Old 30-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,388
Re: What are user defined exceptions in Java?

A user defined exception is a custom made exception class that is given by application provider that must extends exception declare in JAVA API classes. It is used to handle any unconventional error generated by your code. User defined exceptions can be created using try-catch keyword. In this you must write method that is created in parent class Exception.
Reply With Quote
  #4  
Old 30-11-2009
Member
 
Join Date: May 2008
Posts: 2,380
Re: What are user defined exceptions in Java?

following example clear your concept about user defined exceptions


Code:
package com.lea.java;

	  public class UserException extends Exception

	  {

	     public UserException()

	     {

		     super();

	     }
	     
	     public UserException(String str1)

	     {

		      super(str1);

	     }

}
     
'''Use UserException in another class:'''
     
package com.lea.java;

public class CallUserException extends Exception
{

	        static void getException(String n) throws UserException

	        {

		      ///String string2 = "diwanapan";

		    if(n != null)

		    {

			       System.out.println(name + " "+ "You are Welcome to this forum");

		    }

		    else

		    {

			      throw new UserException("Enter your name");

		    }

	}

	public static void main(String[] args) throws UserException

	{

		//CallUserException.getException("diwanapan");

		CallUserException.getException(null);

    }

}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 30-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,844
Re: What are user defined exceptions in Java?

Following example shows how user defined exception is thrown
Code:
public class userException extends Exception
    {
     String message = "";
     int m1;
     public userException()
         {
     }
     public userException(String str1)
         {
         super(str1);
     }
     public String toString()
         {
         if(m1 <= 40)
         message = "You are failed";
         if(m1 > 40)
         message = "You are Passed";
        
         return message;
        
     }
}


public class test
    {
     public static void main(String args[])
         {
         test x = new test();
         x.dd();
     }
     public void dd()
         {
         try
             {
             int p=0;
             if( p < 40)
             throw new userException();
         }
         catch(userException e2)
             {
             System.out.println("my exception"+e2);
         }
     }
}
Reply With Quote
  #6  
Old 30-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,513
Re: What are user defined exceptions in Java?

Code:
using System;

public class UserDefined
{
    public static void Main()
    {
        Summer sum = new Summer();
        try
        {
            sum.DoAverage();
        }
        catch (CountIsZeroException e)
        {
            Console.WriteLine("CountIsZeroException: {0}", e);
        }
    }
}
public class CountIsZeroException: ApplicationException
{
    public CountIsZeroException()
    {
    }
    public CountIsZeroException(string m)
    : base(m)
    {
    }
    public CountIsZeroException(string m, Exception i)
    : base(m, i)
    {
    }
}
public class Summer
{
    int    sum1 = 0;
    int    c = 0;
    float    ave;
    public void DoAverage()
    {
        if (c == 0)
        throw(new CountIsZeroException("Zero count in DoAverage"));
        else
        ave = sum1 / c;
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What are user defined exceptions in Java?"
Thread Thread Starter Forum Replies Last Post
What are the different built-in Exceptions available in java? ScarFace 01 Software Development 5 22-01-2010 08:06 AM
Using Java Exceptions, want to catch the user from entering a negative number MELTRONICS Software Development 5 18-01-2010 04:41 PM
How to create user-defined service on Windows XP killerboy Operating Systems 2 31-07-2009 11:37 PM
end user defined characters Gunter Windows Software 4 13-07-2009 09:36 AM
When to create User Defined Data Type in VB.Net Nihar Khan Software Development 3 27-02-2009 10:29 AM


All times are GMT +5.5. The time now is 12:38 AM.