Skip to main content

JAVA : W1-M6

Exception Handling


Learning Objective

Welcome Back!. In this Module let us solve Problems related to Exception Handling in java.

Orientation Video




Introduction

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. Code of the program :
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);
   }
 }
}
Output of this program: C:\vinod\xml>javac exceptionHandle.java
C:\vinod\xml>java exceptionHandle
For input string: "" is not a numeric value.
An exception is an event that occurs and interrupts the normal flow of instructions. That is exceptions are objects that store the information about the occurrence of errors. When any kind of error or unusual condition occurs, these exceptions are being thrown. Any exception used to occur earlier always resulted in a program crash. However, some programming languages like java have mechanisms for handling exceptions. This is known as catching exception in Java. The exceptions that occur in the program can be caught using try and catch block. Remember, the program will crash if the exception would not be caught. There are three types of exceptions in Java. These are -:
1. Checked Exceptions
2. The error
3. Runtime exception
Error and Runtime exceptions are known as unchecked exceptions. This chapter covers how to throw an exception and catch it. A detailed explanation on the types of exceptions and the advantages of the exceptions.

Resources

Books
  • Java Complete Reference
  • Thinking in Java
Programs for your reference
class except
{
 public static void main(String[] args)
 {  
  double result;
  try
  {
   for(int i=-10; i<=10; i++)
   {   
    result = 100/i;
    System.out.println(result);
   }
  }catch(ArithmeticException e)
  {
   System.out.println("Infinity (divided by zero) ");
  }
 }
}

Task1 Description :

Exceptions
Write a program which also includes the following declarations and try to handle the errors in it with suitable exception handlers which contain nested try catch blocks in a single class. Remember that Never your program should hang.
 1. int num, int denom  , demon= num/0
2. private static BufferedReader buf = new BufferedReader(
   new InputStreamReader(System.in));
   int x = Integer.parseInt(buf.readLine());
3. int c[] = { 1 };
   c[42] = 99;
4. Declare a sting try to print without initialization
5. Try to execute your program in the parent directory observe the output
   and explain the exception.
6. Use ‘finally’ try to print the message from it.
7. Try to put
   catch (Exception e) {
   System.out.println(e.getMessage());
As first catch see the output and explain.

Task2 Description :

Write a Program which does the following:
1.Create a QUEUE data structure with enqueue & dequeue and test the below exception.
2.Create your own exception called QUEUEEMPTY that rises when you try to delete from an empty queue.

Comments