Sunday, January 31, 2021

Exception handling

 Q. What is exception handling?

A: An Exception handling is nothing, it is a mechanism to handle exception and exception handling is done with help of Try-catch and finally block.
Note:Exception raised by try block is caught by catch block and block finally is optional and always executed. 
Q. What is an exception?
A. An Exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.
Q. what is the differences between exception and error classes?
A. Exception can be handled where as error can not be handled.
Q. Why an exception occurs?
A: There can be several reasons that can cause a program to throw exception. For example: Opening a non-existing file in your program, Network connection problem, bad input data provided by user etc.

Q. How many types of exceptions are there?.

A. There are two types of exceptions:

  1. Checked exception
  1. Unchecked exception
Checked exception: These exceptions are optional to handle. Runtime-Exception and Error class and their sub-classes are unchecked exceptions.
Unchecked exception: These exceptions are mandatory to handle. If a methods needs to propagate this exception to its calling method using throw keyword.

OR

Checked exceptions

All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled/declared in the program, you will get compilation error. For example, SQLException, IOException, ClassNotFoundException etc.

Unchecked Exceptions

Runtime Exceptions are also known as Unchecked Exceptions. These exceptions are not checked at compile-time so compiler does not check whether the programmer has handled them or not but it’s the responsibility of the programmer to handle these exceptions and provide a safe exit. For example, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

Q.  what are the difference between Checked exception and Unchecked exception in java?
A: There are many differences:
1.  in Checked exception, try-catch block is mandatory to handle exception where as in Unchecked exception, try-catch block is optional to handle exception.
--------------------------------------------------------------
Points remember:1. In order to handle multiple exceptions in single catch, you can specify multiple exception classes in one catch block by separating them by Pipe(|) character.
Note: catch block of child exception must come first in the order
2. when unchecked exception is propagated from services classes then transaction will be roll backed.
Q. what is the differences between throw and throws keywords?
A. There are many differences:
  1. Throw keyword raise the exception whereas Throws keyword propagate the exceptions.
  1. Throw keyword is used inside a function. It is used when it is required to throw an Exception logically where as Throws keyword is in the function signature. It is used when the function has some statements that can lead to some exceptions.
  1. Throw keyword is used to throw an exception explicitly. It can throw only one exception at a time where as Throws keyword can be used to declare multiple exceptions, separated by comma. Whichever exception occurs, if matched with the declared ones, is thrown automatically.
  1. Checked exception cannot be propagated using throw only.Unchecked exception can be propagated using throw where as For the propagation checked exception must use throws keyword followed by specific exception class name.
  1. Checked exceptions are propagated by throw keyword where as Unchecked exceptions are automatically propagated.
Q: What is exception Propagation?
A: Sometimes a method does not wish to handle an exception and let its calling method to know what exception is occurred. This is called exception propagation.
We can propagate the exception with the help of throws keyword. 

Q. How do you make custom exception?
A.  we make custom exceptions by extending Exception or its sub-classes.
ex: public class AgeValidClass extends Exception{
 public AgeValidClass(){
   super("Your Age is not valid for Vote!");
  }
}
class VoterAge{
public static void main(String[] args) {
try {
findAge(17);
} catch (AgeValidClass e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void findAge( int age) throws AgeValidClass{
 if(age>=18){
System.out.println("Your are welcome for vote!");
}else{
throw new AgeValidClass();
}
}
}

No comments:

Post a Comment

String coding questioon

  How do you reverse a given string in place? ( solution ) How do you print duplicate characters from a string? ( solution ) How do you chec...