Saturday, February 28, 2009

Exceptions:try catch part1

Exception handling techniques:-

Using Try Catch

Exception handling works by transferring the execution of a program to an
appropriate exception handler when an exception occurs. When an unexpected
error occurs in a method ,java creates an object of type Exception .
After creating the Exception object ,Java sends it to the program,by an
action called throwing an exception.The exception object contains information
about the type of error and the state of program when the exception occured.

You need to handle the exception using an exception handler and process application
Therefore,we need a way to tell the JVM what code to execute when a certain exception
happens. To do this, we use the try and catch keywords. The try is used to define a
block of code in which exceptions may occur. This block of code is called a guarded
region (which really means "risky code goes here"). One or more catch clauses
match a specific exception (or group of exceptions—more on that later) to a block
of code that handles it.

Sample code where exception is raised:-

/*public void compute(int num1,int num2){
int result;
reult=num2/num1;
}*/

When we pass two value as 10,0
Aritmetic exception is thrown as

"Exception in thread "main" java.lang.Aritmetic Exception: / zero
at execpt.main(except.java:9)"

An Exception object is created to cause the program to stop and handle
the exception.It expects an exception handler to handle the exception raised.
whenever program is not guarded the default exception handler is invoked.

Basic Try and Catch syntax:-

try{
//serious statements that may cause exception
}
catch(....){
//Exception handler code goes here
}

try:-The try block contains the serious stuff.I the try block governs the statements in which
the chances of getting into exception is more.There is also a point to note that try need atleast
a catch.Both are dependent on each other no one can be used without the other.

NOTE:-the very important point is that a try must be followed by a catch or series of catches
immediately with no other instructions or expression in between.

REMEMBER:-The compiler does not allow any statement between a try block and its associated
catch block.

catch:-The catch statement contains exception handling routines.The catch statement takes the
object of the exception class that refers to the exception caught,as a parameter.Once the
Exception is caught the code within catch will execute.

NOTE:-The scope of catch block is limited to try block only.

Example:-

only Snippet is provided for demo purpose

public void testexcept(int num1,int num2){
int result;
try
{
result=num2/num1;
}catch(ArithmeticException ae){
System.out.println("Exception Divide by zero");
}
System.out.println(result);
}

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP