Home Java Java-exception Exceptional Example in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Exceptional Example in Java

Advertisement
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. 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)

{

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

}

}

}

 

Output in Command Prompt

C:\java>javac ArithmeticExcep.java

C:\java>java ArithmeticExcep

Division by Zero is done

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

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

{

static void throwdemostration()

{

try

{

throw new IllegalStateException();

}

catch (NullPointerException objetB)

{

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

}

}

public static void main(String args[])

{

try

{

throwdemostration();

}

catch(IllegalStateException objetC)

{

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

}

}

}

 

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

C:\java>java ThrowState

Exception Caught in:java.lang.IllegalStateException

C:\java>

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 15, 2008

Related Tutorials

Ask Questions?    Discuss: Exceptional Example in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Vaibhav Mondhe
April 22, 2011
Related to JSP & Servlet

If in HTML form's are different buttons in this button can make perform diffrent action, then what will i do...? Mean 1 button will display some message and other button will reset textboxes in that form. i want this example coding in Servlet or JSP.
subhash
March 9, 2012
to tell some about java

i like java language and i am a begener
sanjai
March 17, 2012
not clear

not so clear for the beginners... Try to explain it more..
Sam
May 4, 2012
Program Error

In the 'Exceptional Example in Java' program there is an error. ''Public Static'' P&S are in caps, where as it should be in lower case. Please note. This wil give error.
bilal
August 27, 2012
error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Arithmetic cannot be resolved to a type at jaleel.ArithmeticException.main(ArithmeticException.java:24)
ramprasad saini
September 19, 2012
java

good example
DMCA.com