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

Thursday, 14 January 2016

Difference between final, finally and finalize in java


final -
final is a keyword.
When a variable declared as final should be initialized only once and it cannot be changed.
In case of classes, final classes cannot be extended
In case of methods, final methods cannot be overridden.


finally -
finally is a block
finally is used in exception handling, is used to place code that are important and it will be executed whether the exception is handled or not.The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.


finalize -
finalize is a method.
Before an object is garbage collected, the run time system calls its finalize() method. That means this method used for clean up processing before object is garbage collected

Sunday, 10 January 2016

Exception Handling in Java

Exception Handling in java is the powerful mechanism to handle run time errors in a java program.It is an abnormal condition in a program running cycle. 

Mainly Exceptions are of checked and unchecked exceptions.

Advantages of Exceptions handling:

statement1;
statement2;
statement3; // Exception occurs
statement4;

Then the fourth statement will not execute, instead if we handled the exception then the next statement will execute.

Types of exceptions:

As mentioned above there are two types of exceptions:
Checked and Unchecked Exceptions

Checked Exceptions:
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.

Unchecked Exceptions:
The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime. 

Common Scenarios for exceptions:

  1. int n=20/0;//ArithmeticException  
  2. String a=null;  System.out.println(a.length()); //NullPointerException  
  3. String a="one";  int i=Integer.parseInt(a);//NumberFormatException  
  4. int a[]=new int[5];  a[10]=50//ArrayIndexOutOfBoundsException 
Implementing exceptions:
Java try-catch and Java try-finally blocks

Syntax of try- catch
  1. try{  
  2. //code that may throw exception  
  3. }
  4. catch(Exception_class_Name ref){
  5. }  
Syntax of try-finally block
  1. try{  
  2. //code that may throw exception  
  3. }finally{}  

Example1:

public class sample{
  public static void main(String args[]){
   try{
      int n=20/0;
   }catch(ArithmeticException e){System.out.println(e);}
   System.out.println("rest of the code...");
}

Example2:

class sample2{
  public static void main(String args[]){
  try{
   int n=45/5;
   System.out.println(n);
  }
  catch(NullPointerException e){System.out.println(e);}
  finally{System.out.println("finally block is always executed");}
  System.out.println("rest of the code...");
  }
}  

Above mentioned above will give an idea on what is exceptions and exception handling in java.

Thursday, 23 January 2014

BASIC finally EXCEPTION HANDLING IN JAVA

Exceptions:The finally Block 

Example to show Finally exception in java

The Finally block in java is used along with the try-catch statements.This block executed even after the unexpected exception occured.The Run time always execute the expression in finally block irrespective of the try block.  The main usage of finally block is to do clean up job. Keeping cleanup code in a finally block is always a good practice, even when no exceptions are occured. The runtime system always executes the code within the finally block regardless of what happens in the try block. So it is the ideal place to keep cleanup code.

throws JAVA KEYWORD

Java:Using throws in Exception Handling

How to throws exceptions in java with example

The throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.

Wednesday, 22 January 2014

NESTED try STATEMENT

NESTED try IN EXCEPTION HANDLING


Java:nested try-catch blocks with simple example

The try block within a try block is known as nested try block.Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested, one within another. In java, this can be done using nested try blocks. A try statement can be inside the block of another try. In a nested try block, every time a try block is entered the context of that exception is pushed on the stack.

MULTIPLE catch CLAUSES IN JAVA

HANDLING MULTIPLE catch CLAUSES
Java Exception Handling:catching multiple exceptions

The code bound by the try block need not always throw a single exception. If in a try block multiple and varied exceptions are thrown, then you can place multiple catch blocks for the same try block in order to handle all those exceptions. When an exception is thrown it traverses through the catch blocks one by one until a matching catch block is found.

USING try AND catch

How to use exception handling keywords try and catch in java

THE try BLOCK


A try statement is used to catch exceptions that might be thrown as your program executes. You should use a try statement whenever you use a statement that might throw an exception,That way,your program won’t crash if the exception occurs.The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block. 

EXCEPTION HANDLING IN JAVA

how to use exception handling

Exception-Handling Fundamentals

An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error.When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error.That method may choose to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed.