Exceptions in Java

Exceptions in Java are classified on the basis of the exception handled by the java compiler. Java consists of the following type of built in exceptions:

  1. Checked Exception:- These exceptions are the object of the Exception class or any  of its subclasses except Runtime Exception class. This condition arises due to invalid input, problem with your network connectivity and problem in database. java.io.IOException is a checked exception. This exception is thrown when there is an error in input-output operation. In this case operation is normally terminated.

List of Checked Exceptions-

Following are the list of various checked exception that defined in the java. lang package.

                         Exception                    Reason for Exception
 ClassNotFoundException This Exception occurs when Java run-time system fail to find the specified class mentioned in the program
Instantiation Exception This Exception occurs when you create an object of an abstract class and interface
Illegal Access Exception This Exception occurs when you create an object of an abstract class and interface
Not Such Method Exception This Exception occurs when the method you call does not exist in class
  1. Unchecked Exception:- These Exception arises during run-time that occurs due to invalid argument passed to method. The java Compiler does not check the program error during compilation. For Example when you divide a number by zero, run-time exception is raised.
                          Exception                   Reason for Exception
Arithmetic Exception These  Exception occurs, when you divide a number by zero causes an  Arithmetic Exception
Class Cast Exception These Exception occurs, when you try to assign a reference variable of a class to an incompatible reference variable of another class
Array Store Exception These Exception occurs, when you assign an array which is not compatible with the data type of that array
Array Index Out Of Bounds Exception These Exception occurs, when you assign an array which is not compatible with the data type of that array
Null Pointer Exception These Exception occurs, when you try to implement an application without referencing the object and allocating to a memory
Number Format Exception These Exception occurs, when you try to convert a string variable in an incorrect format  to integer (numeric format) that is not compatible  with each other
Negative ArraySizeException These are Exception, when you declare an array of negative size.

User Defined Exceptions

As we come across Built -in-exception, you create own customized exception as per requirements of the application. On each application there is a specific constraints. For Example, In case of Air Flight Booking, a passenger must specify the source ,destination place and age. Passenger must brings a ticket who is above the age of   3 years. Similarly in the case of a banking application, A Customer whose age is less than Eighteen need to open Joint account.Thus,Error-handling become necessary while developing a constraint application The Exception class and its subclass in java is not able to meet up the required constraint in application. For this, you create your own customized Exception to over address these constraints and ensure the integrity in the application.

 

How to Handle and create User-defined Exception

The keywords used in java application are try, catch and finally are used in implementing used-defined exceptions. This Exception class inherits all the method from Throwable class.

List of Method defined by Throwable  Class

                         Methods            Explanation 
String toString( ) Gives you a String object and description of the exception. This  methods is called by the println ( ) method when an object of throwable is passed to it as argument.
String getMessage( ) Gives you the description of the exception in program
Throwable fillInStackTrace( ) Gives you a Throwable Object that contains a stack trace.
void print StackTrace( ) Gives you and print the stack trace
void printStackTrace(PrintStream stream) Return the stack trace to a specific defined stream
String getLocalizedMessage Return the Localized description of the exception


Understand User-Defined Exceptions with Example-

In the given below code, the UseDefinedException, subclass of  the Exception class is created. The UseDefinedException class has one constructor,i.e UseDefinedException( ).

The String toString( ) method returns you string containing the description of the exception. The Test Class defines the method mm that throw an object of UseDefinedException, The Exception is thrown when marks is less than 50.

Creating a User Defined Exception

class UseDefinedException extends Exception{
String msg = “”;
int marks;
public UseDefinedException() ){

}
public UseDefinedException(String str){
super(str);
}
public String toString(){
if marks <= 50)
msg = “You have failed”;

if marks > 50)

msg = “You have Passed”;
return msg;
}
}
public class test{
public static void main(String args[]){
test t = new test();
t.mm();
}
public void mm(){
try{
int i=0;

if( i < 50)
throw new MyException();

}
catch(MyException ee1){
System.out.println(“my ex”+ee1);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s