X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

User or Custom Defined Exceptions :

User defined exceptions are those which are developed by Java programmer as a part of application development for dealing with specific problems such as negative salaries, negative ages, etc.

Guide lines for developing user defined exceptions :


  • Choose the appropriate user defined class.

  • Every user defined class which we have choose must extends java.lang.RuntimeException class.

  • Every user defined sub-class exception must contain a parameterized constructor by taking String as a parameter.

  • Every user defined sub-class exception parameterized constructor must called by parameterized constructor of java.lang.RuntimeException class by using super (String parameter always).

Example :

import java.util.*; class Algo extends RuntimeException { Algo(String s) { super(s); } } class Valley extends RuntimeException { Valley(String s) { super(s); } } class Example { public static void main (String[ ] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter any number : "); int a = Integer.parseInt(sc.nextLine()); if(a > 0) { throw new Algo("Positive Number"); } if(a < 0) { throw new Valley("Negative Number"); } else { System.out.println("Invalid Number"); } } }

Output:

Enter any number : 1 Exception in thread "main" Algo: Positive Number at Example.main(Example.java:25) Note : main() method should not throw any exception since the main() method is called by JVM and JVM cannot provide user friendly message. It is highly recommended to keep our customized Exception class as unchecked i.e. we have to extend RuntimeException class but not Exception class while defining our customized exceptions.

Based on the resource who triggers the exception, all exceptions are divided into two types :

  • JVM Exceptions




  • Programmatic Exceptions




JVM Exceptions :-

The exceptions which are raised automatically by the JVM whenever a particular event occurs are called JVM Exceptions. Examples : ArrayIndexOutOfBoundException , NullPointerException etc.


Programmatic Exceptions :-

The exceptions which are raised explicitly either by the programmer or by the API developer are called Programmatic Exception. Examples : IllegalArgumentException , NumberFormatException etc.


Some important exceptions :


ArrayIndexOutOfBoundException :-

It is the child class of RuntimeException and hence it is unchecked, raised automatically by the JVM whenever we are trying to access array element outside the range index.

Example :
	  
int[ ] a = new int[10];
System.out.println(a[0]); // 0
System.out.println(a[10]); // Runtime Exception : ArrayIndexOutOfBoundException

NullPointerException :-

It is the child class of RuntimeException and hence it is unchecked, raised automatically by the JVM whenever we are trying to perform any operation on null.

Example :
	  
String s =  null;
System.out.println(s.length()); // Runtime Exception : NullPointerException

StackOverflowError :-

It is the class of Error and hence it is unchecked, raised automatically by the JVM whenever we are trying to perform recursive method invocation.

X

invocation :

It refers to the action of invoking (call on) someone or something.

Example :
	  
class f
{
 public static void m1()
 {
   m2();
 }
 public static void m2()
 {
   m1();
 }
 public static void main (String[ ] args)
 {
   m1();
 } 
} // Runtime Exception : StackOverflowError

NoClassDefFoundError :-

It is the child class of Error and hence it is unchecked, raised automatically by the JVM whenever JVM unable to find required class.

Example :

java Example
Example.class file is not available then we will get Runtime Exception saying NoClassDefFoundError. 

ClassCastException :

It is a child class of RuntimeException and hence it is unchecked, raised automatically by JVM whenever we are trying to typecast parent object to the child type.

Example :
	  
String s = new String("Algo");
Object o = (Object) s; // valid

Object o = new Object();
String s = (String) o; // Runtime Exception : ClassCastException

ExceptionInInitializerError :-

It is a child class of Error and hence it is unchecked, raised automatically by JVM if any exception occurs while performing initialization for static variables assignment and while executing static block.

Example :
	  
class Example
{
 static int a = 10/0;
 public static void main (String[ ] args)
 {

 }
} // Runtime Exception : java.lang.ExceptionInInitializerError
                         Caused by: java.lang.ArithmeticException: / by zero  
	  
class Example
{
 static
 {
    String s = null;
    System.out.println(s.length());
 }
 public static void main (String[ ] args)
 {

 }
} // Runtime Exception : java.lang.ExceptionInInitializerError
                         Caused by: java.lang.NullPointerException

IllegalArgumentException :-

It is the child class of RuntimeException and hence it is unchecked, raise explicitly by the programmer or API developer to indicate that a method has been involved with invalid argument.

Example :
	  
Thread t = new Thread();
t.setPriority(10); // valid
t.setPriority(11); // Runtime Exception : IllegalArgumentException

NumberFormatException :-

It is the child class of RuntimeException and hence it is unchecked, raised explicitly by the programmer or by API developer to indicate that we are trying to convert String to number type but the String is not properly formatted.

Example :
	  
int a = Integer.parseInt("10"); // valid
int b = Integer.parseInt("Ten"); // Runtime Exception : NumberFormatException

IllegalStateException :-

It is the child class of RuntimeException and hence it is unchecked, raised explicitly by the programmer or by the API developer to indicate that a method has been invoked at inappropriate time.

After starting a thread, we are not allowed to restart the same thread otherwise we will get Runtime Exception : IllegalThreadStateException

Example :
	  
Thread t = new Thread();
t.start();
t.start(); // Runtime Exception : IllegalThreadStateException

AssertionError :-

It is the child of Error and hence it is unchecked, raised explicitly by the programmer or by API developer to indicate that assert statement fails.

Example :

int x = 10;
assert(x > 10);
System.out.println(x); // Runtime Exception : AssertionError

Exception propagation :

The process of delegating the responsibility of exception handling from one method to another method by using throws keyword is called exception propagation.


Rethrowing exception :

We can use this approach to convert one exception type to another exception type.

Example :

class Example
{
 public static void main (String[ ] args)
 {
   try
   {
     System.out.println(10/0);
   }
   catch(ArithmeticException e)
   {
     throw new NullPointerException();  
   }
 }
} // Runtime Exception : NullPointerException


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.