Java exception handling

Explain the need of Exception handling.

1. A program rarely executes without errors for the first time.

2. Users run applications in unexpected ways

3. A program should handle these abnormal situations

4. Exception handling allows to handle the code without cluttering the original code

5. Exception handling allows error handling code to be authored in a separate block known as ‘catch’

6. Exception handling separates the functional code with error handling code

7. Some exception handling process can be diverted to JVM

Explain the Exceptions categories, i.e. checked and unchecked exceptions.

Unchecked exceptions

1. The defects in the programs are represented
2. Passing invalid arguments to a method without parameters
3. They reflect errors in applications

Checked exceptions

1. They represent conditions that are invalid in certain areas outside of the program, such as invalid user input, files absence, network outrages.
2. All these exceptions are subclasses of Exception class
3. Developer has over all control to handle them
4. Imposes robustness in applications

Provide the general form of Exception handling constructs with explanation

The constructs are trying, catch, finally, throw and throws

1. try block: the source of an exception
2. catch block: the exception handling block
3. finally block: the code in finally block gets executed whether an exception is caught and handled or not

The following code illustrates the use of try, catch and finally
public void openFile()
{
   FileReader reader = null;
   try
   {
       reader = new FileReader("stock");
       int number=0;
       while(number != -1)
       {
           number = reader.read();
           System.out.println((char) number );
       }
   }
   catch (IOException excp)
   {
       // exception handling code goes here
   }
   finally
   {
       if(reader != null)
       {
       try
       {
           reader.close();
       }
       catch (IOException e)
       {
           //code to handle if file not found goes here
       }
       }
   System.out.println("--- File End ---");
   }
}
throw : to explicitly throw an exception based on the application’s demand
throws : diverts the responsibility of exception handling to JVM

What is user defined Exception? Explain with an example.

1. A user defined exception is extended by Exception class.

2. Unconventional actions are dealt with user defined exceptions

3. Application demanded exceptions other than APIs are defined as user defined exceptions.
Example :
When the balance of an account is below zero after withdrawl, an exception can be raised like ‘NegativeBalanceException’

4. As every exception returns a message, a user defined exception should also return a message.

The following code snippet depicts the user defined exception
public class MyException extends Exception
{
   String msg = "";
   int marks;
   public MyException(String str)
   {
       super(str); // the super class constructor should receive the message
   }
   public String toString()
   {
       if(marks <= 40)
       msg = "You have failed"; // assigning a message
       if(marks > 40)
       msg = "You have Passed";
       return msg; // the message is returned when the exception is raised
   }
}

public class Sample
{
   public static void main(String args[])
   {
       Sample sample = new Sample();
       sample.myMethd();
   }
   public void myMethod()
   {
   try
   {
       int marks=0;
       if( marks < 40)
       throw new MyException(); // exception object is returned to the catch block
   }
   catch(MyException myExcp)
   {
       System.out.println("The exception is"+myExcp);
   }
   }
}
When the exception raises, the message “You have failed “ will be sent to the Exception class constructor and displays it.
Java multithreading
Java multithreading - What is Multithreading? Explain the life cycle of a thread, Explain how to use thread class for Multithreading in Java, What are the methods of thread class in java?, Can your explain thread priorities?...
Java file handling
Java file handling - Use of Streams, Difference between Stream classes and Reader writer classes, Explain and demonstrate the use of File, RandomAccessFile classes, Explain the use of Reader and Writer classes...
Java utility classes
Java utility classes - What is object Serialization? Explain the use of Persisting object, step of using object Deserialization...
Post your comment