Making Exceptions Unchecked - java tutorial,java tutorials

making exceptions unchecked

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

Checked and Unchecked Exception

In this section, you will learn Checked and Unchecked Exceptions in java and how to handle it. The Exception is a condition which indicates error in your code.

Types of built-in exception in Java

Given below types of built-in exception in java :

1. Unchecked Exception

2. Checked Exception.

Unchecked Exception

You don't need to import java.lang package since it is implicitly available in all java programs. The exceptions derived from 'RuntimeException' are readily available. Due to this, these exception need not to include in any methods 'throws' list. These exceptions are called 'Unchecked Exception'. Some of the common examples are : ArithmeticException, NullPointerException, ClassCaseException.

Checked Exception

These Exceptions are defined by 'java.lang' and must be include in a methods 'throws' list. These exceptions are known as Checked Exception.
Given below the complete List of Checked Exception :

  • ClassNotFoundException
  • CloneNotSupportException
  • IllegalAccessException
  • InstantiationException
  • InterruptedException
  • NoSuchFieldException
  • NoSuchMethodException

Handling Exception in Java

In java exception is handled through the five keywords : try, catch, throw, throws and finally. The code which you are suspect of can put into try block to monitor for exception. Using catch, you can handle the exception. Each try block need at least one catch or a finally clause. For throwing exception explicitly, we use keyword throw. Any known exception that is thrown out of a method must be specified as such by throws clause. Generally checked exceptions are thrown using throws clause.

try and catch example :


public class TryCatch {
public static void main(String args[]) {
int d, a;
try {
d = 0;
a = 42 / d;
System.out.println("This will not be printed" + a);
} catch (ArithmeticException e) {
System.out.println("Divided by zero");
}
System.out.println("After catch statement");
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac TryCatch .java

C:\Program Files\Java\jdk1.6.0_18\bin>java TryCatch
Divided by zero
After catch statement

Throw :

You can throw exception explicitly as follows :

throw ThrowableInstance ;

ThrowableInstance must be an object of type 'Throwable' or a subclass of 'Throwable'.

Example :


public class ThrowDemo {

	static void DevProc() {
		try {
			throw new NullPointerException("demo");
		} catch (NullPointerException e) {
			System.out.println("Caught inside Devproc.");
			throw e;
		}
	}

	public static void main(String args[]) {
		try {
			DevProc();
		} catch (NullPointerException e) {
			System.out.println("Recaught :" + e);
		}

	}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac ThrowDemo .java

C:\Program Files\Java\jdk1.6.0_18\bin>java ThrowDemo
Caught inside Devproc.
Recaught :java.lang.NullPointerException: demo

throws :

If a method is capable of causing exception but it is not using 'catch' to handle this. The 'throws' specify this behaviour so that callers of the method can guard themselves from that exception.

Example :


public class ThrowsDemo {
	static void throwone() throws IllegalAccessException {
		System.out.println("Inside throwone");
		throw new IllegalAccessException("demo");
	}

	public static void main(String args[]) {
		try {
			throwone();
		} catch (IllegalAccessException e) {
			System.out.println("Caught " + e);
		}
	}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac ThrowDemo .java

C:\Program Files\Java\jdk1.6.0_18\bin>java ThrowDemo
Caught inside Devproc.
Recaught :java.lang.NullPointerException: demo

finally

finally creates a block of code that will be executed after a try/catch block, and it will execute whether or not an exception is thrown. This can be useful for closing file handles and freeing up any other resources that might have been allocated.

Note :- The finally clause is optional . However each try block need at least one catch or a finally clause.

Example :

public class FinallyDemo{

   public static void main(String args[]){
      int x[] = new int[2];
      try{
         System.out.println("Element three :" + x[3]);
      }catch(ArrayIndexOutOfBoundsException e){
         System.out.println("Exception thrown  :" + e);
      }
      finally{
         x[0] = 6;
         System.out.println("First element: " +x[0]);
         System.out.println("The finally statement is executed");
      }
   }
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac FinallyDemo.java

C:\Program Files\Java\jdk1.6.0_18\bin>java FinallyDemo
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed

Advertisements

Share on Google+Share on Google+

Making Exceptions Unchecked - java tutorial,java tutorials

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Making Exceptions Unchecked - java tutorial,java tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Mahesh Babu
June 26, 2012
Exception

NoSuchMethod Exception is example for checked or unchecked exceptions.please give correct answer.

Ads

 

Ads