Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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. 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>

 

                         

» View all related tutorials
Related Tags: c exception io stack type exec uri lock this block exe row handle program to execution ram each pos ast

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.