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