User Defined Exceptions.

In this article,User Defined Exceptions are explained in detail with examples.
To create user-defined exceptions we inherit our class from any one of the predefined-exception-class
Ex
Userdefined Exception-javaform
Now your class also becomes exception class,it also has 3 characteristics. (From topic User Defined Exceptions)
  • Exception-type.
  • Exception-Message.
  • Exception-state.
Ex
//program related to User Defined Exceptions.
class OutofRange extends Exception
{
OutofRange(String ss)
{
super(ss); //here ss becomes "Message" of your user-defined exception
}
}
public class Exception5
{
public static void main(String args[])
{
int i=10;
try
{
if(i<=10)
throw new OutofRange("10 is the limit");
}
catch(OutofRange o)
{
//System.out.println("Error is:"+o.getMessage());
//System.out.println("Error is: "+o);
o.printStackTrace();
}
System.out.println("Hello world");
}
}
Output
UserdefinedException-javaform

Note
Exception caught in the catch block.
  • To print only exception message,we use "getMessage()" method.This method is inherited from the Throwable class.
  • To print Exception type and Exception-message, we use just "Exception-object" for printing.
  • To print or display Exception-type,Exception message and Exception-state (all three characteristics),we use "PrintStackTrace" method.This method is inherited from the Throwable class.  (From topic User Defined Exceptions)

Handling Related User Defined Exceptions in Hierarchy.
When related exceptions are caught using multiple-catch-blocks,first catch sub-class exceptions and then followed by base-class exceptions(i.e, bottom to top in the hierarchy).

Exception Hierarchy-Javaform
Ex
//program related to User Defined Exceptions.
class Except0 extends Exception
{
}
class Except1 extends Except0
{
}
class Except2 extends Except1
{
}
class Except3 extends Except2
{
}
public class Exception71
{
public static void main(String args[])
{
int i=3; //by changing i values, output will be thrown to different exceptions.  
try
{
if(i==0) throw new Except0();
if(i==1) throw new Except1();
if(i==2) throw new Except2();
if(i==3) throw new Except3();
}
catch(Except3 e)
{
System.out.println("Exception3");
}
catch(Except2 e)
{
System.out.println("Exception2");
}
catch(Except1 e)
{
System.out.println("Exception1");
}
catch(Except0 e)
{
System.out.println("Exception0");
}
}
}
Output
Handling Related User defined Exception-javaform

Exception Propagation.
Whenever an exception occurs in your method and there is no solution then in such case,we have to send that exception back to the calling method.This is known as "Exception-propagation".
It is done as follows. (From topic User Defined Exceptions.)
public void mymethod() throws (Exception type)
{
---
(exception-occurred-but-no-solution-);
------
}
Ex
//program related to User Defined Exceptions--Exception propagation.
public class Exception10
{
public static void main(String args[])
{
System.out.println("Inside main() calling method1()");
method1();
System.out.println("End of the main() program ");
}
public static void method1()
{
System.out.println("Inside method1() calling method2()");
method2();
System.out.println("End of the method1()");
}
public static void method2()
{
System.out.println("Inside method1() calling method3()");
try
{
method3();
}
catch(ArithmeticException ae)
{
System.out.println("Division by Zero Illegal operation");
}
System.out.println("End of the method2()");
}
public static void method3()
{
System.out.println("Inside method3() calling method3()");
int i=1,j=0,k;
k=i/j;
System.out.println("End of the method3()");
}
}
Output
Exception propagation-javaform
                            To get the code file use me.

Finally block
It is a block of code, used at the end of all the catch blocks.
Ex
try
{
}
catch() //catch1
{----}
catch() //catch 2
{----}
catch() //catch3
{----}
catch() //catch 4
{----}
finally
{----}
This block is executed whatever is the situation in try block i.e, whether exception is raised or not.
Ex
//program related to User Defined Exceptions--Finally Block
class UnknownException extends Exception
{
}
public class Exception121
{
public static void main(String args[])
{
int x=2;
while(true)
{
System.out.println("A");
try
{
System.out.println("B");
if(x==1)
return;
System.out.println("C");
if(x==2)
System.exit(0);
//break;
System.out.println("D");
if(x==3)
continue;
System.out.println("E");
if(x==4)
throw new UnknownException();
} //end-to-try
catch(UnknownException e)
{
System.out.println("Exception occured");
}
finally
{
System.out.println("\t Finally Block");
}
System.out.println("G");
} //end-of-while
//System.out.println("H");
}//end-of-main()
}
Output
finallyblock-javaform
                              To get the code file use me.


Nested Try Block
using a particular try block inside another try-block.
Ex
try //outer-try-block
{
try //inner-try-block
{
---
}
catch(---) //inner-catch-block
{
--
}
}
catch(---) //outer-catch-block
{
----
}
Here, outer try block exceptions are caught by outer catch block and inner-try-block exceptions are caught by inner-catch-block only.
Ex
//program related to User Defined Exceptions--NestedTryBlocks.
class NestedTryBlocks
{
public static void main(String args[])
{
try
{
System.out.println("Inside outer try block");
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
try
{
System.out.println("Inside Inner try block");
int z=x/y;
System.out.println("Z is:"+z);
}
catch(ArithmeticException e)
{
System.out.println("Unable to Divide");
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Invalid Number of Arguments");
}
catch(NumberFormatException ne)
{
System.out.println("Please enter proper numbers");
}
}
}
Output
NestedTryBlocks-javaform
                            To get the code file use me.

Continue to the next topic Exception Handling.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing 

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required