Making Exceptions Unchecked - java tutorial,java tutorials

making exceptions unchecked

Making Exceptions Unchecked - java tutorial,java tutorials

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

Tutorials

  1. Assertion in java
  2. Anonymous Inner Classes - Anonymous Inner Classes tutorial
  3. Appending Strings - Java Tutorials
  4. Assertion in Java
  5. Autoboxing unboxing in Java - Java Tutorials
  6. Thread Deadlocks - Java Tutorials
  7. BASIC Java - Java Tutorials
  8. Interthread Communication in Java
  9. boolean comparisons - tutorial
  10. Catching Exceptions in GUI Code - Java Tutorials
  11. Exception in Java - Java Tutorials
  12. Causing Deadlocks in Swing Code
  13. Class names don't identify a class - Java Tutorials
  14. Commenting out your code - Java Tutorials
  15. Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java
  16. Disassembling Java Classes - Java Tutorials
  17. Double-checked locking,java tutorials,java tutorial
  18. Exceptional Constructors - Java Tutorials
  19. Final Methods - Java Tutorials
  20. garbage collection in java
  21. Java - JDK Tutorials
  22. J2EE Singleton Pattern - Design Pattern Tutorials
  23. Java Comments - Java Tutorials
  24. Java Field Initialisation - Java Tutorials
  25. Java HashSet  - Java Tutorials
  26. Java Multi Dimensions Array - Java Tutorials
  27. java awt package tutorial
  28. Java GC
  29. Java HashMap - Java Tutorials
  30. JDK 1.4 the NullPointerException - Java Tutorials
  31. HashMap and HashCode
  32. LinkedHashMap - Java Tutorials
  33. Which is Faster - LinkedList or ArrayList?
  34. Making Enumerations Iterable - JDK 5 Example
  35. Making Exceptions Unchecked - java tutorial,java tutorials
  36. Creation Time Comparison of Multi Dimensional Array- Java Tutorials
  37. Multicasting in Java - java tutorials,tutorial
  38. Non-virtual Methods in Java - java tutorials
  39. Orientating Components Right to Left,java newsletter,java,tutorial
  40. The link to the outer class,java tutorial,java tutorials