Exceptional Example in Java

When an error occurs in a method, Java creates an object of the Exception method. On making an object of exception method, java send it to the program by throwing the object of exception method.

Exceptional Example in Java

When an error occurs in a method, Java creates an object of the Exception method. On making an object of exception method, java send it to the program by throwing the object of exception method.

Exceptional Example in Java

Exceptional Example in Java

     

When an error occurs in a method, Java creates an object of the Exception method. On making an object of exception method, java send it to the program by throwing the object of exception method. The Exception Object states the information of type of error and status of program during the exception Occurred.

Following are the Keyword used in implementing Exceptional-Handling-

  1. Try
  2. Catch
  3. Throw
  4. Throws
  5. Finally

How to implement Try-Catch Block

The Try Block contain a set of statements  that raise an exception-event within  the scope of  method.  If the exception arises in Try-Block. the appropriate handler with the Try Block proceed the exception.

As We Know, the Catch Block is used as exceptional-handler. The Exception that arises in the try block is handled by the Catch -Block. Each Try block preceded by one Catch block. The catch block specifies the type of exception need to catch.

Let Us Understand With Example

class Exception Unhandled

{

Public static void main(String args[])

{

int num1=0; 

int num2=8;

int num3=num2/num1;

System.out.println("The num3  =  " +num3);

}

 

The above code contain a class Exception Unhandled, Which on Compile gives us Arithmetical exception with no exception being handled .In this code the Java run-time a exception when a number is divided by zero. The output of the code shows that the exception thrown is the object of the subclass of exceptional class.

Output in Command Prompt


C:\Documents and Settings\Administrator>cd\

C:\>cd new folder\

C:\New Folder>javac ExceptionUnhandled.java

C:\New Folder>java ExceptionUnhandled
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionUnhandled.main(ExceptionUnhandled.java:7)

 

See How we going to implement Try-Catch Block in the above coding

Inorder to handle exception ,you need to implement the Try-Catch. If an exception arises within a try block, the appropriate exception handler  associated with try block handles the exception. The Catch block catches the object of the exception class as a parameter. Once the exception is caught in catch-block, the expression within the corresponding block is executed.

class ArithmeticException

{

public static void main(String args[])

{

int num1 = 0;

int num2=10;

int num3=0;

try

{

num3=num2/num1;

System.out.println("The result =" +num3);

}

catch(ArithmeticException e) 0

{

System.out.println("Division by Zero is done");

} 1

}

}

 

Output in Command Prompt 2

C:\java>javac ArithmeticExcep.java

C:\java>java ArithmeticExcep

Division by Zero is done 3

Using the Throw

The throw an exception can be explicitly used by using the keyword throw statement. For example you have entered wrong username and password in login form need to thrown an exception. The throw expression normally causes the code to stop or terminate during normal flow of java code.The Throw proceed the controls to nearest catch block. If  there is no catch block, then program terminates.

The Syntax used to declare the throw statement:

throw Throwable objt 4

The above Syntax ,Throwableobjt is an object of the class Throwable.The Throwableobj object is created  using the new operator.The compiler gives you an error if the throwableobjt does not belong to a valid class of Exception.

The following Example help you to understand  the throw -

class ThrowState 5

{

static void throwdemostration()

{ 6

try

{

throw new IllegalStateException(); 7

}

catch (NullPointerException objetB)

{ 8

System.out.println("Not caught by catch block inside throwdemostration().");

}

} 9

public static void main(String args[])

{

try 0

{

throwdemostration();

} 1

catch(IllegalStateException objetC)

{

System.out.println("Exception Caught in:"+ objetC); 2

}

}

} 3

 

In the above given code, the new operator create an object of IllegalStateException.In the throwdemostration() method, an exception IllegalStateException is thrown. 

Output of the code in Command Prompt

C:\java>javac ThrowState.java 4

C:\java>java ThrowState

Exception Caught in:java.lang.IllegalStateException

C:\java> 5