Pageviews

Showing posts with label Exception handling. Show all posts
Showing posts with label Exception handling. Show all posts

Wednesday, 19 June 2013

Keywords used in Exception handling in Java

by Unknown  |  in Exception handling at  Wednesday, June 19, 2013
Exception handling in Java uses five keywords,unlike C++ which uses 3 keywords i.e. try,catch,throw.Java uses 5 following keywords to handle all types of Exceptions.


  • try
  • catch
  • throw
  • throws
  • finally


1.try block:In the try block,the code which we know that would create exceptions are written in try block.It must be used within the method and it must be followed by finally or catch block.

Syntax:           
            try
      {
       The code that may generate an exception is placed in this block.
        } 


 2.catch : A exception which is thrown by try block must be brought either in catch block or finally block.Once the exception is found in the try block,it is always by the Catch block.

        Syntax:            


 catch(Exception_name objectname)
{
 Statements/ error to display to user when exception occour.
  }


3 throw: Whenever an exception occur,throw block throws the exception to the catch block. Each exception that occur must be caught, the type of exception generated is caught by the catch block of that particular exception, catch block is declared below the try block.

           Syntax:       


 try
 {
 The code which may generate an exception is placed in this block.
 } 
 catch(Exception_name objectname)
{
 Statements to display to the user when an exception will occur.

  }   

4 throws: throws keyword gives a method flexibility of throwing an Exception rather than handling it. with throws keyword in method


5 finally : This block executes in every situation even the exception occur or not.finally create a block of code that will be executed after a try catch block is executed completely. If an exception is thrown, finally block will execute even if no catch statement matches exception.


Syntax: finally
{
Block of statements which are to be executed
}

Here is the simple program to explain these keywords:

WAP in Java to implement Exception handling

import java.io.*;    //This package is imported when we use Exception handling
class excep2{
public static void main(String args[])
{
try                                                                      //try block
{
int r[]=new int[5];
r[7]=10;
System.out.println("The array is"+r[7]);
}
catch(ArrayIndexOutOfBoundsException exc )                //catch block
{
System.out.println("You are specifying an Array Index which is out of Bound");
}
finally                                                                             //finally block
{
System.out.println("Please check your array size");
}
}
}


The output goes here:


Exception handling























Monday, 17 June 2013

Exception handling in Java

by Unknown  |  in Exception handling at  Monday, June 17, 2013
Exception handling is one of the most powerful features supported by high level programming languages.Java also support exception handling by providing 5 keywords to handle all kinds of exceptions.

Exception handling






Error:An Error is a condition in the program which may produce undesirable result or it may hinder the normal execution of the program.Errors,basically are of 3 types:

1.Compile time errors:The errors which ccurs during thecompilation of the program are called as Compile time errors.Eg:Variable not declared,Loop not initialized etc.

2.Run Time errors:The errors which occurs during the run time of a program are known as Run time errors.These errors interrupts the normal flow of execution of the program. Eg:divide by 0,Array index out of bound.

3.Logical errors:These types of errors are produced by the programmer itself,because our program gets compiled and run smoothly,but the error is in the logic of the program.Eg:If we used < insted of > or used / instead of \.

Exception handling in Java:Exceptions are the unwanted entities in our program,that interrupt the normal flow of program.Example:

Statement1;
Statement2;
Statement3;
Statement4;
Statement5;
Statement6;
Statement7;
Statement8;
Statement9;
Statement10;

Our program consists of 10 statements,if the error is in the statement 5,then the rest of the statements will not be executed,the program flow will be halted.Java supports Exception handling to control these exceptions.Some exceptions are automatically handled,but some are handled using the special keywords used in Exception handling.

Types of Exception handling:


  • Built in Classes
  • User defined exeptions


Built in Classes: The classes which are already defined in the Java are called as Built in exception classes.The root class for exceptions is Exception.All the exception classes are defined in the class exception.

User defined Exceptions:The exception classes which are defined by the user by inheriting the root exception class are known as User defined exception classes in Java. 

Proudly Powered by Blogger.