Sunday 2 April 2017

Exception Handling

Exception handling is quite frequent in interviews and you can expect good questions on this topic. Exception handling is integral part of java programming. Having sound knowledge of exception handling concepts would help you to write more stable code.

1. What is an Exception in java? 

Answer: Exception is the special event in program which stops program normal execution. Your program can not continue it's execution once exception occurred.
For example consider below program in which we are performing division operation. If user gives 0 as divisor, since division by 0 is undefined hence your program would simply throw an exception to let you know something wrong happened.
1
2
3
4
5
6
7
8
9
public class TestDivideByZeroException {
 public static void main(String[] args) {
  divide(10,2);
  divide(12, 0); //Here we will get java.lang.ArithmeticException: divide by zero
 }
 public static void divide(int dividend, int divisor){
  int quotient = dividend/divisor;  //This line is likely to throw a divide by zero exception.
 }
}

2. How to handle an exception? 

AnswerAs I mentioned above in case of exception your program can not continue it's execution. So in order to let program continue it's execution from very next line, Java has the concept of exception handling like any other programming language.

Exception is defined as an object in Java. Exception object actually carries information related to exception event which help programmers to know more about exception like exception source etc.

So we can say that whenever any exception occurs in your program, Java creates an object having information related to exception.

Follow-Up Question: Could you please discuss about Try/Catch mechanism?

Answer: To handle exceptions Java has try/catch mechanism. It involves three blocks:
1. try block: In try block we write the code(business logic) which is likely to throw exception. Like in this below example we kept line number 9 inside try block.
2. catch block: In this block we handle the exception this is the block which gets called if try block throws any exception.
3. final block: In this block we write the finalize codes. Stuffs that we want to do at the end regardless of whether exception occurs or not like closing DB connection, closing files etc.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class TestDivideByZeroException {
 public static void main(String[] args) {
  divide(10,2);
  divide(12, 0); //Here we will get java.lang.ArithmeticException: / by zero
 }
 public static void divide(int dividend, int divisor){
  int quotient;
  try{
   quotient = dividend/divisor;  //This line is likely to throw an divide by zero exception.
  }catch(ArithmeticException exception){
   exception.printStackTrace();
  }finally{
   quotient = 0;
  }
 }
}

3. When finally and catch block executes?

Answer
catch block only executes if try block throws any exception. If your code inside try block executes fine without any exception then catch block will never get executed. 
finally block executes always irrespective of exceptions in try block, So in both the scenario mentioned below finally block will execute:
1. If try block throws an exception then catch block will get executed first and then final block will get executed.
2. If try block doesn't throw any exception then catch block won't get executed instead final block will get executed directly.

If you run below code you will get output in console as "In finally block" for first method call(line number 3) as there is no exception. For second method call(line number 4) you will you will get both the output in console as "In finally block " and "In catch block" as it throws exception.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class TestDivideByZeroException {
 public static void main(String[] args) {
  divide(10,2);
  divide(12, 0); //Here we will get java.lang.ArithmeticException: / by zero
 }
 public static void divide(int dividend, int divisor){
  int quotient;
  try{
   quotient = dividend/divisor;  //This line is likely to throw divide by zero exception.
  }catch(ArithmeticException exception){
   System.out.println("In catch block");
  }finally{
   System.out.println("In finally block");
  }
 }
}

4. What will be the final value of "quotient" variable in below example?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class TestDivideByZeroException {
	public static void main(String[] args) {
		divide(12, 0); //Here we will get java.lang.ArithmeticException: divide by zero
	}

	public static void divide(int dividend, int divisor) {
		int quotient;
		try {
			quotient = dividend / divisor; // This line is likely to throw an divide by zero exception.
			return;
		} catch (ArithmeticException exception) {
			quotient = 2;
			System.out.println("In catch block");
		} finally {
			quotient = -1;
			System.out.println("In final block");
		}
	}
}

Answer:  Value of quotient will be -1.


Explanation: First try block will execute and will throw exception which will invoke catch block. In catch block value of quotient will get updated to 2 at line 12. Method will return from here (at line 10) and then control will pass to finally block and value of quotient will get updated to -1.

We can say that finally block execute always irrespective of return statement or any other statements in try or catch block.
This is the reason we use finally block for resource cleaning like closing DB connection, closing files and other important resources.
Note: Please note that if we put System.exit() at line number 10 in above code instead of return in that case finally block won't get executed. This is the only scenario where finally block doesn't execute.

5. Explain exception class hierarchy?

Answer: Throwable is super class for all kind of exceptions. We can divide Throwable in two categories:
1.Error: It is occurrence of abnormal event during program execution which is not in the control of programmer. It is very difficult for program in execution to recover from an error. For example if JVM runs out of memory or stack over flow error.
2.Exception: It has two sub categories : Checked exceptions and Unchecked exceptions(also called Run time exception).


Fig : Exception class hierarchy.

6. What is different between Checked Exceptions and Unchecked exceptions? 

Answer: Checked Exceptions are those exceptions which programmers can't control or we can say that checked exceptions does not occurs because of programmer's error.
For example If there is a program which reads data from a file located in a particular location. Program will throw FileNotFoundException if file won't be present in given location then it is not programmer's error.
Designers of Java decided to force programmers to guard their program against possible checked exception. So if your code likely to throw a checked exception then compiler will show compile time error to force you either catch the error or throw it up in method call hierarchy.
For example in File IO scenarios Compiler knows when you try to read or write a file what are the possible exceptions that can occurs.
Examples:
1.Reading/writing To/From file can lead to FileNotFoundException hence compiler will force you to catch the exceptions or throw it.
2.IO operation can lead to IO device failure etc. hence compiler will force you to catch the exceptions or throw it.
3.SQLException etc.

Note: Checked Exceptions are also known as Compile time exceptions because compiler knows at compile time about the possible exception that program can throw.


Unchecked Exceptions are those exceptions which programmers can control or we can say that Unchecked exceptions occur purely because of coding error.

For example NullPointerException is an Unchecked exception which occurs because of making call on null object. We know creation of object happens at run time that's why compiler can not predict possible NullPointerException or any other Unchecked exception. Unchecked exceptions are also known as Runtime Exception.
As we understood so far that compiler can not predict possible Unchecked exceptions for a given program. It is programmer's responsibility to check for possible Runtime(Unchecked ) exceptions and rectify them in code itself even before they occur at run time.
We should not try to catch Runtime exceptions as it is not standard practice (even we can catch them as Runtime exception are also exceptions). It will make your code unnecessarily complex and hard to read.
Example:
1.NullPointerException,
2.ArrayIndexOutOfBoundException etc.

Note: Unchecked Exception are called Runtime exceptions as well because compiler can not predict at compile time about the possible 
Unchecked exceptions that program can throw.

7. What is an Error?
Answer It is abnormal event in program execution which is not in control of programmers. It is very difficult for a program in execution to recover from an error. Also it is not good practice to catch the error (again even we can also catch errors as they are also subclass of Throwable). We should always avoid to catch any Error.
Examples
1. OutOfMemoryError: It occurs when JVM runs out of memory, 
2. StackOverflowError: It occurs when Stack runs out of memory.

Follow-Up Question: Can we catch an error?

Answer: Yes, We surely can catch anything which is subclass of Throwable. We already know that Error is sub class of Throwable.
So we can say that all kind of errors in java we can catch in our program.
Note: It is not advisable to catch errors in program since most of errors are very serious problems and in most of the cases you can't do anything for those errors. For example OutOfMemoryError you should not suppose to catch because by catching it you are not solving the issue, just making your program more complex.  



8. Can a program recover from StackOverflowError?

Answer: Yes, If we catch StackOverflowError in program our program will easily recover from error.
So all we need to do only catch the StackOverflowError.

Follow-Up QuestionNow Interviewer will ask how only catching the exception will solve your issue since your stack is already full?

AnswerLet's First discuss the code below We are recursively calling badMethod() without any terminal condition in order to produce StackOverflow Error. So it is like an infinite loop.

In Code 1 as you can see we are calling badMethod() without try/catch which leads to StackOverflowError and program will get terminated without calling goodMethod(). 
Where as in Code 2 we are catching the error and we are getting output as "Hello" since this time goodMethod() is getting executed.
Code 1:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class TestStackOverflow {
 public static void main(String[] args) {
  badMethod(); //Will get StackOverFlowError here.
  goodMethod(); // It won't get executed as program terminates by throwing error.
 }
 
 static void badMethod(){
  badMethod();
 }
 
 static void goodMethod(){
  System.out.println("Hello");
 }
}

Code 2:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class TestStackOverflow {
 public static void main(String[] args) {

  try {
   badMethod();
  } catch (StackOverflowError er) {

  }
  goodMethod(); // It will execute as we handled Error. }

 static void badMethod() {
  badMethod();
 }

 static void goodMethod() {
  System.out.println("Hello");
 }
}

Now Let's discuss how just catching the error solved StackOverFlowError: Whenever we make a method call in program Java pushes method into stack and whenever method return(successful or exception) Java pops method from stack.
In above code we are doing infinite recursive method call which will eventually use all stack space. When JVM will encounter with stack full situation it will immediately propagate an exception which will pass on up in method call hierarchy to check for some one who can handle it and if JVM won't find any handler then program will throw error to user and terminate. In this process after every propagation in method call hierarchy Java pops method from stack which will create spaces in stack and by handling error we gave chance to our program to continue it's execution as stack is free now.

9. Which exception caller function will receive in below code?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.io.FileNotFoundException;
import java.io.IOException;

public class TestStackOverflow {
 public static void main(String[] args) throws IOException {
  badMethod();
 }

 static void badMethod() throws IOException {
  int quotient;
  try{
   quotient = 1/0;  //This line will throw divide by zero exception.
   return;
  }catch(ArithmeticException exception){
   throw new IOException();
  }finally{
   throw new FileNotFoundException();
  }
 }
}

Answer: Caller function will get FileNotFoundException. IOException which try block is throwing will lost because after throwing exception control will pass to finally block which is throwing FileNotFoundException.

No comments:

Post a Comment