Saturday 3 August 2013

29:Checked and Unchecked exception

Learning Objectives
After completing this session, you will be able to:
‰  Identify Checked and Unchecked exception
‰  Create your own exception
‰  Describe Run time exception
Checked and Unchecked Exceptions
Checked exception:
‰  Java compiler checks if the program either catches or lists the occurring checked
exception
‰  If not, then compiler error will occur
Unchecked exceptions:
‰  Not subjected to checking at the timeof compilation for exception handling
‰  Built-in unchecked exception classes:
oError
oRuntimeException
oTheir subclasses
‰  Handling all these exceptions may make the program cluttered and may become a
nuisance
Creating Your Own Exception Class
Steps to follow for creating your own exception class are:
1.Create a class that extends the RuntimeException or the Exception class
2.Customize the class: Members and constructors may be added to the class
Example:
1 class HateStringExp extends RuntimeException {
2 /* some code */
3 }
How to use Your own Exceptions
1 class TestHateString {
2 public static void main(String args[]) {
3 String input = "invalid input";
4 try {
5 if (input.equals("invalid input")) {
6 throw new HateStringExp();
7 }
8 System.out.println("Accept string.");
9 } catch (HateStringExp e) {
10 System.out.println("Hate string!”);
11 }
12 }
13 }
Try It Out
Problem Statement:
Write a program that illustrates the creation and usage of your own exceptions.
Code:
import java.io.*;
public class ExceptionTest {
public static void main(String args[]) {
boolean finished = false;
do {
try {
processUserInput();
} catch (VowelException x) {
System.out.println("A vowel exception
occurred.");
} catch (BlankException x) {
System.out.println("A blank exception
occurred.");
} catch (ExitException x) {
System.out.println("An exit exception
occurred.");
finished = true;
} finally {
System.out.println("This is the finally
clause.\n");
}
} while (!finished);
}
}
How It Works:
‰  The ExceptionTest program reads a character entered by the user.
‰  It then throws and catches a VowelException, BlankException, or ExitException based
on the input of the user
‰  ExceptionTest provides two static methods, main() and processUserInput() .
‰  The main() method consists of a simple do statement that repeatedly tries to invoke
processUserInput() .
‰  The try statement has three catch clauses and a finally clause.
‰  The three catch clauses notify the user of the type of exception they catch.
‰  The catch clause with an ExitException parameter causes the do statement and the
program to terminate by setting finished to true.
‰  The finally clause just displays the fact that it has been executed.
‰  The processUserInput() method prompts the user to enter a character.
‰  The actual reading of the character occurswithin a try statement. IOException is
caught by the try statement, eliminating the need to declare the exception in the
processUserInput() throws clause.
‰  The IOException is handled by notifying the user that the exception occurred and
continuing with program execution.
‰  The processUserInput() method throws one ofthe three exceptions based upon the
character entered by the user.
‰  If the user enters a vowel, then VowelException is thrown.
‰  If the user enters a line beginning with a character that cannot be printed, then
BlankException is thrown.
‰  If the user enters x or X, then ExitException is thrown.
‰  When you run ExceptionTest, it produces the following prompt:
oEnter a character:
oEnter a blank line, and the following output is displayed:
oA blank exception occurred.
oThis is the finally clause.
oEnter a character:
‰  The program notifies you thata blank exception has occurred and displays the fact
that the finally clause of the main() try statement was executed.
‰  The processUserInput() method, upon encountering a space character returned by
getChar() , throws the BlankException, which is caught by the main() method.
‰  The finally clause always executes no matter whether processUserInput() throws an
exception or not.
‰  Enter a at the program prompt, and the following output appears:
oEnter a character: a
oA vowel exception occurred.
oThis is the finally clause.
oEnter a character:
‰  Here the program notifies you that a VowelException has occurred. The processing of
the VowelException is similar to the blank exception.
oEnter j, and the following is displayed:
oEnter a character: j
oThis is the finally clause.
oEnter a character:
‰  No exceptions are thrown for the j character, but the finally clause is executed.
‰  The finally clause is always executed, no matter what happens during the execution of
a try statement.
‰  Go ahead and type x to exit the ExceptionTest program. The program displays the
output as shown below:
oEnter a character: x
oAn exit exception occurred.
oThis is the finally clause.
‰  The program then returns you to the command prompt.
‰  The output acknowledges the fact that the Exit Exception was thrown by
processUserInput() and caught by main().
Summary
‰  You can create your own exceptions, normally by extending Exception or one of its
subtypes. Your exception will then be considered a checked exception, and the
compiler will enforce the handle or declare rule for that exception.
‰  All catch blocks must be ordered from most specific to most general. If you have a
catch clause for both IOException and Exception, you must put the catch for
IOException and Exception, you must put the catch for IOException first in your code.
The compiler will stop you from defining catch clauses that can never be reached.
‰  Some exceptions are created by programmers, some by the JVM.
Test Your Understanding
1.State true or false for the following:
a)In Java, exceptions are divided into two categories, namely checked and
unchecked exceptions.
b)All subclasses of the RuntimeException and Error classes are unchecked
exceptions.

No comments:

Post a Comment