Exception Classes in Java With Example

0
586
Spread the love
  • 67
  • 99
  • 55
  • 45
  •  
  •  
  •  
  • 18
  •  
  •  
    284
    Shares

ClassCastException is thrown from a program to indicate that the code has attempted to cast an object to a subclass of which it is not an instance or it could be due to incorrect type.
However it is possible to avoid this exception by checking the type first using instanceOf operator or method.

The structure of the class ClassCastException is as follows:


public class java.lang.ClassCastException extends java.lang.RuntimeException{
//constructor
public ClassCastException();
//constructs an empty ClassCastException object that is an object with no message specified.
public ClassCastException(String message);
//constructs an ClassCastException object with the message specified.
}
 

Let us check some example: In this example assume that Class ClassA has two child classes ClassB and ClassC.
ClassA
/           \
ClassB ClassC

class ClassA{
}
class ClassB extends ClassA{
void MethodB()
{
....
}
}
class ClassC extends ClassA{
void MethodC()
{
....
}
}
class Test{
void castTest()
{
//possible
ClassA a new ClassA();
ClassB b=(ClassB)new ClassA();
ClassA b=new ClassB();
ClassA a1=new ClassC();
//not possible
ClassB b1=new ClassC();
ClassB b2=new new ClassA();
ClassC c1=new ClassB();
}
} 

ArrayStoreException in Java:

ArrayStoreException is thrown out of a program to indicate that an attempt to store the wrong type of object into an array of different type of objects.

The structure of the class ArrayStoreException as follows:


public class java.lang.ArrayStoreException extends java.lang.RuntimeException{
//constructor
public ArrayStoreException ();
//constructs an empty ArrayStoreException object that is an object with no message specified.
public ArrayStoreException (String message);
//constructs an ArrayStoreException object with the message specified.
}

Example of ArrayStoreException:

This exception occurs when one tries to store a value in an array of an incompatible class or type.


void wrongArrayStore()
{
int storeArray=new int[15];
boolean booArray=new boolean[5];
System.arrayCopy(storeArray,2,booArray,2,4);
}
 

The output of the code will be an ArrayStoreException.

EmptyStackException in java

EmptyStackException is thrown by the stack class to indicate that the stack is empty and there is no element present to pop out from the stack.

The structure of EmptyStackException is given by


public class java.util.EmptyStackException extends
             java.lang.RuntimeExceprion{
//constructor
public EmptyStackException();
//constructs a new EmptyStackException with no details message.
}
 

Class NoSuchElementException in Java

NoSuchElementException is thrown by mostly nextElement() method of an enumeration to indicate that there are no more elements in the Enumeration (such as vector) or the stack is empty.

The structure of NoSuchElementException class is given by

public class java.util.NoSuchElementException extends
             java.lang.RuntimeException{
//constructors
public NoSuchElementException();
//default constructor and constructs a NoSuchElementException with no details message.
public NoSuchElementException(String message);
//constructs a NoSuchElementException with the message passed in the argument
}

StringOutOfBoundsException in Java:

StringOutOfBoundsException is thrown from an application’s chatAt(index i) method in class String or by other String method,when tried to access a character that is outside the boundary or the bounds of a String. or a StringBuffer. This is mainly to indicate that an index is either negative ,equal to the size of the String or greater than the size of the String.

The structure of the class StringOutOfBoundsException is given by:

public class java.lang.StringOutOfBoundsException extends 
                                                  java.lang.IndexOutOfBoundsException{
//constructor
public StringOutOfBoundsException();
//constructs an empty StringOutOfBoundsException object that is an object with no message specified.
public StringOutOfBoundsException(String message);
//constructs an StringOutOfBoundsException object with the message specified.
public StringOutOfBoundsException(int index);
//constructs an StringOutOfBoundsException object with the argument indicating the illegal index.
} 

Example of StringOutOfBoundsException

-An attempt to access a non existent character position in a String.

public void myStringMethod()
{
String myString="Love";
char myChar=myString.charAt(7);
} 

SecurityException in Java:

SecurityException is thrown from an application to indicate that a security violation has occurred. Security Manager throws this exception. It indicates that an requested operation is not permitted due to security reasons.This is mostly applicable for Applet.

This exception is thrown by some methods in system package when one attempts to call a method that will perform ac action not allowed by current security settings of the browser.(This happens when java code tried to run in Applet inside a browser).

This can also be thrown if the program denies permission when prompted to allow an action such as -writing to a file.It is very difficult(almost impossible ) for a hostile applet programmer to create a virus(potentially unsafe). If we try to do something like this, this would violet security inside of an applet and this exception is thrown.

The structure of the SecurityException is given below:


public class java.lang.SecurityExceptionextends java.lang.RuntimeException{
//constructor
public SecurityException();
//constructs an empty SecurityException object that is an object with no message specified.
public SecurityException(String message);
//constructs an SecurityException object with the message specified.
}

Class NullPointerException in Java

NullPointerException is thrown from an application when our code attempts to use null in the case where an object is required.
Below can be the scenarios where NullPointerException can occur:

  1. Calling the instance method of the/a null object.
  2. Accessing or modifying the field of the null object.
  3. Tracking the length of null as if it was empty.
  4. Accessing or modifying the slots of null as if it was an array.
  5. Throwing null as if there was a Throwable value
  6. Attempt to access a field of null object
  7. Attempt to invoke a method of a null object.

Applications should throw instances of this class to indicate other illegal uses of the null object.

The structure of NullPointerException class is given as :


public class java.lang.NullPointerException extends java.lang.RuntimeException{
//constructor
public NullPointerException();
//constructs an empty NullPointerException object that is an object with no message specified.
public NullPointerException(String message);
//constructs an NullPointerException object with the message specified.
}

So when an attempt to use a method or a variable that contains a null object reference,this NullPointerException occurs.

Example Of NullPointerException

public void myMethod(){
String myString=null;
if(myString.equals("abc")){
System.out.println("Hello");
}
} 

output of the code:
NullPointerException

Class NoSuchMethodException in Java

NoSuchMethodException is thrown to indicate that the object of a class does not have the method called.This exception is very rarely thrown as the existence of methods is primarily determined during compile time. We can get this exception if we remove a method from a class,recompile the class without dependency tracking and then run a program that calls the removed method.

The structure of the class NoSuchMethodException is given as :

public class java.lang.NoSuchMethodExceptionextends java.lang.Exception{
//constructor
public NoSuchMethodException();
//constructs an empty NoSuchMethodException object that is an object with no message specified.
public NoSuchMethodException(String message);
//constructs an NoSuchMethodException object with the message specified.
}

NegativeArraySizeException in Java:

NegativeArraySizeException is very popular built in exception classes in java and is thrown by an application when the code tries to create an array with negative size. In other words, an attempt has been made to allocate an array with less than zero elements.

The structure of the class NegativeArraySizeException  is given as :

public class java.lang.NegativeArraySizeException extends java.lang.RuntimeException{
//constructor
public NegativeArraySizeException();
//constructs an empty NegativeArraySizeException object that is an object with no message specified.
public NegativeArraySizeException(String message);
//constructs an NegativeArraySizeException object with the message specified.
}

Class InterruptedException in Java

InterruptedException is thrown from an application when a thread is waiting ,sleeping or otherwise paused for a long time and another thread interrupts it using the interrupt() method defined in class Thread.In effect, it is a way to break out those methods before they complete their normal cycle.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 67
  • 99
  • 55
  • 45
  •  
  •  
  •  
  • 18
  •  
  •  
    284
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here