Results 1 to 6 of 6

Thread: What are user defined exceptions in Java?

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    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;
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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);
    
        }
    
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    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);
             }
         }
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    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;
        }
    }

Similar Threads

  1. What are the different built-in Exceptions available in java?
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:06 AM
  2. Replies: 5
    Last Post: 18-01-2010, 05:41 PM
  3. How to create user-defined service on Windows XP
    By killerboy in forum Operating Systems
    Replies: 2
    Last Post: 31-07-2009, 11:37 PM
  4. end user defined characters
    By Gunter in forum Windows Software
    Replies: 4
    Last Post: 13-07-2009, 09:36 AM
  5. When to create User Defined Data Type in VB.Net
    By Nihar Khan in forum Software Development
    Replies: 3
    Last Post: 27-02-2009, 11:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,533,408.70339 seconds with 17 queries