Thursday, 25 June 2015

Exception Handling in java

No one can perfect in the world human beings are make mistake,software developers also made mistake while developing the code. When there is a mistake in program code that rises a error and causes the abnormal termination of the program.

There are types of errors generally
  • Run-time errors
  • Compile time errors
  • Logical errors 

Run-time errors: These are the errors that occurs during the run time of program such as divide by zero and opening a non existed file ext..

Compile Time errors: These the are errors that occurs due to insufficient memory and reading elements out of the array bounds...

Logical errors: These are the error that occurs due to missing semicolon, misspelled variables in functions, incomparable parameters ext..

Then What is an Exception ? 


                                            In java an exception is an object  that causes the abnormal termination of the program.Generally all the exceptions are run time errors and they are caused by JVM. That is the errors are detected at run tine and those are called checked exceptions and those are detected at run time time are called unchecked exceptions and errors are another category of this type and it is not possible to write code and handle these categories.

                                           when ever an exception will occur either it can be handled or thrown by the JVM for other process.Java uses five keywords for handling exceptions: try,throw,catch,finally,throws. when every the code leads to an an error that can be placed in the try block and thrown using throw and catches for handling other wise that can be throws to finally block. Here we are throwing the exception object only. When ever there is a solution and it returns to place where the exception arise.

Try and catch blocks  


The two primary keywords for exception handling are try and catch. The try block is used where we except the code going to be create an exception/ Whenever an exception is occurred and the try block follows the catch block. The following are the general syntax for java exception handling.

try

{

// you java statements capable of throwing exceptions

}

catch(Exception_1 e)

{

// java statements to handle exception_1 thrown from try


}

finally

{

// final solution to problem/code

}


Try Block

                  When the run time error will occur in the program that may be thrown by the java virtual machine .a try block is a group of statements that are enclosed with in {} and starts with a try keyword.The try block is followed by an appropriate handler called catch block for handling the exceptions thrown by try. If there is no exception then the try block executes and return the result.

                                                                         It is very important a try block must followed by one or more catch blocks/one or more finally blocks other wise it gives and error message like " try without catch or finally". exception handling is done by using or more try block or one can use nested try blocks.

Catch Block

                               A catch block is groups of statements which are used to handle the exception thrown from the try block.As shown above the catch block should place immediately after the try block.When ever an exception occur in try, the control transfer to catch block by receiving an exception object of type Throw able class or it's subclasses.


What is Finally?
                         The finally is keyword used in exception handling. The finally block is conclusion of the try. It is used for clean up the program before control is passed to different parts of the program.

Why Finally ?

                     Whenever an exception occurs the exception affects the flow of execution of the program. Sometimes some blocks may be bypassed by exception handling. Consider a program, which has allocated memory to a particular variable with an intention to use it the deallocation of memory for that variable is at the end of the code.
                                Now, Suppose that due to the uncaught exception the program execution has bypassed the deallocation part of code. This is undesirable and therefore in order to make sure that some statements must be surely executed irrespective of exception. The finally keyword designed to address this contingency.

                          The statements within the finally block execute surely, irrespective of whether the exception occurred in try block or not. This finally block is useful in closing file, deallocating  memories etc.Every try block must be associated at least one catch or finally.

The following program demonstrate how finally works.

class FinallyDemo
{
static void expone()
{
try
{
System.out.println(" exception one");
throw new RuntimeException("FinallyDemo");
}
finally
{
 System.out.println(" exception one finally executed");
}
}
Public static void main(String args[])
{
try
{
expone();
}
catch(Exception e)
{
  System.out.println(" caught the exception");
}
}
}

                        In the above program irrespective of whether the  try block rises an exception or not, the finally block get executed. That is the finally is used mostly to cleaning up  the code before the program control goes to other part of the program.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top