Exception Handling

What is an Exception?

Definition:

“When an unusual, irregular, abnormal or nonstandard condition arises in a sequence of code at run time this phenomenon is referred as Exception handling

An exception is a runtime error.

In Object Oriented, an exception is an object that is used to describes an exceptional condition that has occurred in a piece of code.

When an exception arises, an object that is rejavasenting that exception is created and thrown in the method that caused the error.

  • That method may be used to choose and to handle the exception itself, or it may pass on.
  • Either way, at some point the exception is caught and processed.

Some Fundamentals of Exception Handling

Exceptions can be generated by the Java runtime system, or they can be manually generated by in the code, by the user himself.

Exceptions that thrown by Java relate to fundamental errors that abuse or violate the rules of the Java language or the constraints of the Java execution environment

Manually generated exceptions are typically used to report some error condition to the caller of a method.

Keywords

Following are the five keywords that are used to manage exception in java programming. Those are explained as under

  • try:

Instructions in a program that we want to monitor for exceptions are contained in a try block

  • catch:

Code that can catch an exception (using catch) and can handle it is contained in catch block.

  • throw:

The keyword throw is used to manually throw an exception.

  • throws:

Any exception that is thrown out of a method must be specified as such by a throws clause

  • finally:

Any code that absolutely must be executed before a method returns is put in a finally block

Structure of an exception

Below is the basic form of an exception-handling block:

  1. Try
  2. {
  3.     //block of code to monitor for errors
  4. }
  5.     catch(ExceptionType1 exceptionObj)
  6. {
  7.     //exception handler for ExceptionType1
  8. }
  9.     catch(ExceptionType2 exceptionObj)
  10. {
  11.     //exception handler for ExceptionType2
  12. }
  13.     //…
  14.     Finally
  15. {
  16.     //block of code to be executed before try block ends
  17. }

Types of Exception

In java there is a built in class i.e. Throwable, all exception types are subclasses of this Throwable class.
Throwable have two subclasses that partition exceptions into two distinct branches

Following are the two types of Exceptions

  1. Headed by Exception
  2. topped by Error
  3. run time exception

1. Headed by exception

This class is used for exceptional conditions that user programs should catch.

This is also the class that we will subclass to create our own custom exception types.

2. Topped by Error

It defines exceptions that are not expected to be caught under normal circumstances by our program.

Exceptions of type Error are used by the Java runtime system to indicate errors having to do with the runtime environment, itself, e.g., Stack overflow.

3. Run time exception

This is an important subclass of Exception.
Run time Exceptions are automatically defined for the programs that we write and include things such as division by zero and invalid array indexing.

Problem when we do not handle Exceptions

It is very important for user to use Exceptions in the program.
Let’s see what happens when we do not use exceptions.

Example without Exceptions :

  1.   class ExceptionByZero
  2.   {
  3.         public static void main(String args[])
  4.     {
  5.           int d = 0;
  6.         Int a = 42 / d;
  7.     }
  8.   }
  9.  

When we run the above code error will come in our program, so any number divided by Zero becomes equal to infinity. So it will generate an error like:

“Unknown Exception in the main thread”

Example with Exceptions

  1.   class ExceptionTryCatch
  2.   {
  3.          public static void main(String args[])
  4.   {
  5.          int a, b;
  6.         try { // monitor a block of code.
  7.           a = 0;
  8.           b = 42 / a;
  9.          System.out.println("This will not be printed.");
  10.          }
  11.         catch (ArithmeticException e)      
  12.         System.out.println("Division by zero.");
  13.          }
  14.             System.out.println("After catch statement.");
  15.          }
  16.    }
  17.  
  18.  

Comments

hi how are you ? i want the code of (b8Controls4.ocx,ctrlNSDataCombo.ocx) my adress email [email protected]

this is good work but you put more code for this web site it is more essential for new comers in java world

The tutorial is excellent. Please and more for newbies like me. Akinyemi

Add new comment