Exception Handling

* What is Exception?
Ans.: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.There are two categories of exceptions and Errors are not the kind of exceptions:
  1. Checked Exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.
  2. Runtime Exceptions: A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.                                                                                                            
  3. Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.
*What is the Exception Hierarchy?
Ans.: All java exceptions inherit from Exception class.This Exception class also inherit from Throwable class.

+ Only throwable objects can be use with the exception handaling machanism.
+ RuntimeException from package java.lang
+ IOException from package java.io
+ Both of these represent exceptional situations that can occur in java program that can be caught by application
+ Class error and its subclasses represent abnormal situations that could happen in the JVM.
+ Errors happens infrequently and should not be caught by application.
+ It is usually not possible for applications to recover from Errors.
 

*What is throw and throws?

If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.

You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the different in throws and throw keywords.

import java.io.*; 
public class className { 
 public void deposit(double amount) throws RemoteException { 
       // Method implementation 
       throw new RemoteException();
   } //Remainder of class definition 
}

* What is finally block?
* What is User Defined exception?
Reactions

Post a Comment

0 Comments