java4all@1986 java. Powered by Blogger.
Showing posts with label Exception handling. Show all posts
Showing posts with label Exception handling. Show all posts

Exception Handling In java?

>> Tuesday, June 21, 2011

Exception Handling:   Exception, that means exceptional errors. Actually exceptions are used for handling errors in programs that occurs during the program execution. During the program execution if any error occurs and you want to print your own message or the system message about the error then you write the part of the program which generate the error in the try{} block and catch the errors using catch() block. Exception turns the direction of normal flow of the program control and send to the related catch() block. Error that occurs during the program execution generate a specific object which has the information about the errors occurred in the program.

In the following example code you will see that how the exception handling can be done in java program. This example reads two integer numbers for the variables a and b. If you enter any other character except number ( 0 - 9 ) then the error is caught by NumberFormatException object. After that ex.getMessage() prints the information about the error occurring causes.

   
import java.io.*;

public class exceptionHandle{
  public static void main(String[] args) throws Exception{
  try{
  int a,b;
  BufferedReader in = 
  new BufferedReader(new InputStreamReader(System.in));
  a = Integer.parseInt(in.readLine());
  b = Integer.parseInt(in.readLine());
  }
  catch(NumberFormatException ex){
  System.out.println(ex.getMessage() 
  + " is not a numeric value.");
  System.exit(0);
  }
  }
}
The output will as follow
1).If we enter are trying to write a value other than integer it throws error
I.e Entering a as value it throws,For input string: "a" is not a numeric value.

Read more...

Important points in Exception Handling?

>> Monday, May 23, 2011

1.If we have try with multiple catch blocks ,the order of catch blocks must be from child to parent .
2.We can handle the Exceptions by using try and catch blocks.
3. Throws keyword can be used ,when the programmer doesn't want to handle the Exception and throws it out the method.
4.Throw keyword can be used , if the programmer want to handle an Exception Explicitly and throw in catch block.
5.try----> to maintain risky code.
6.catch---> to maintain Exception handling code.
7.finally------>to maintain clean code.
8.Throwable is the super class of all exceptions and errors.

Read more...

Difference Between Final,Finally and finalize()?

Final:
1.It is a modifier applicable for classes,methods and variables.
2.for the final classes we can't create child class that is inheritance is not possible.
3.Final methods can be overridden in child class.
4.for final variables re-assignment is not possible.

Finally:
1.It is a block associated with try,catch .
2.The main objective of finally block is to maintain clean-up code, which should be execute always.

Finalize:
1. It is a method ,should be execute by the garabage collector,just before destorying a object












Read more...

Method's to display error information

1.)printStackTrace: This error method display information as below
Name of Exception:Description
Stack trace

2.)toString():This error method display information as below
Name of Exception:Description

3.)getMessage:This error method display information as below
Description.

Read more...

Exceptions

1.What is meant by Exception?
It is an unwanted or an unexpected event which disturbs normal flow of a program.

2.Difference Between Checked Exception And Unchecked Exception?
Checked Exception:The Exception which are checked by compiler at compilation time.
Unchecked Exception: The exception which are checked by JVM at Runtime.

3.ERRORS:There are unrecoverable,they due to system resources but not due to lack of program.

4.)Difference Between Partially checked and fully checked Exception?
An exception is said to be partially checked exception if some of it's child classes are checked.
An exception is said to be partially unchecked exception if and only if all it's child classes are checked.

Read more...

Top Ten Exceptions in Java?

>> Sunday, May 22, 2011

  1. Null Pointer Exception Exception
Assigning null to an object.
EXAMPLE:
String s-null;

2.Stack Overflow error

If we are performing recursive method invocation
Example:
class Test{

public void m1()
{
m1();
}
public static void main(String args[])
{
m1();

}


}

3.ArrayIndexOutOfBound Exception
If we are assigning a value to an array which is out of it's range

EXAMPLE:
int[] a={10,20,89,60}
System.out.println(a[2]); o/p is==89;
System.out.println(a[5]); o/p AIOBE;

4.) ClassCastException
It occurs when we are trying to typecast parent class object to child type

5.) NoclassDefFoundError
If the required class is not found available
Ex:
Java A and enter
If a.class file is not available we will get NCDFE

6.ExceptionInInitializerError
Whenever an Exception occurred during Initialization of static variables (or) static blocks.
EX:
class Test
{
static int i=10/0;
}

output :
EIIE caused by AritmeticException/by Zero

7.)IllegalArgumentException
If we are trying to invoke set priority method with more than it's range
EX:
class Test{
public static void main(String args[])
{
Thread.currentThread.setPriority(100);
}
}

8.) NumberFormatException
We are attempting to convert string to number type,if string is not formatted properly
EX:
String s="10";
int i=Integer.parseInt(s) o/p===10;

String s="ten";
int i=Integer.parseInt(s); o/p is NFE

9.)IllegalStae Exception

If thread is started and we again start the thread it raises ISE

10.)Assertion Exception

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP