top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Exception Handling in Java

0 votes
302 views

Causes of Exceptions

An exception is an abnormal condition that arises out of an extraordinary situation disrupting the flow of program’s instructions. Exceptions report error conditions in a program.

                                    

In programs, exceptions can occur due to any of the following reasons:

  • Programming errors: These exceptions arise due to errors present in APIs, such as NullPointerException. The program using these APIs cannot do anything about these errors.
  • Client code errors: The client code attempts operations, such as reading content from a file without opening it. This will raise an exception. The exception will provide information about the cause of the error and is handled by the client code.
  • Errors beyond the control of a program: There are certain exceptions that are not expected to be caught by the client code such as memory error or network connection failure error. These are errors which have been raised by the run-time environment.

                                                                                      

Classification of Exceptions

When an error occurs in a method during runtime, an object is created containing information about the error, its type and the state of the program. This object is referred to as exception object. Exception objects are instances of classes that are derived from the base class Throwable.

In Java there are two types of exceptions. They are:

  • Checked Exceptions: These types of exceptions are derived from the Exception class. The program either handles the checked exception or moves the exception further up the stack. It is checked during compilation.
  • Unchecked Exception: These exceptions are derived from the RuntimeException class, Which in turn is derived from the Exception class. The program need not handle unchecked exception. It is generated during the execution of the program.

 

Types of Checked Exceptions

All checked exceptions are derived from the Exception class. Some of the common checked exceptions are:-

InstantiationException: This exception occurs if an attempt is made to create an instance of the abstract class.

InterruptedException: This exception occurs if a thread is interrupted.

NoSuchMethodException: This exception occurs if the java virtual Machine is unable to resolve which method is to be called.

RuntimeException: This exception may be thrown during normal operation of java virtual machine if some erroneous condition occurs.

 

Types of Unchecked Exceptions

All Unchecked exceptions are directly or directly or indirectly derived from the RuntimeException class. Some of the common Unchecked exceptions are described:

ArithmeticException: Derived from RuntimeException and indicates an Arithmetic error condition.

ArrayIndexOutOfBoundsException: Derived from RuntimeException. Method receives an illegal arugument.

NegativeArraySizeException: Derived from RuntimeException. Array size is less than zero.

NullPointerException: Derived from RuntimeException. Attempt to access a null object member.

NumberFormatException: Derived from IllegalArgumentException. Unable to convert the string to a number.

StringIndexOutOfBoundsException: Derived from IndexOutOfBoundsException. Index is negative or greater than the size of the string.

posted Nov 6, 2017 by Frank Lee

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Event Handling

Event handling is fundamental to java programming because it is integral to the creation of applets and other types of GUI-based programs. Events are supported by a number of packages, including java.util, java.awt, and java.awt.event.

Most events to which your program will respond are generated when the user interacts with a GUI-based program. These are the types of events, they are passed to your program in a variety of ways, with the specific method dependent upon the actual event. There are several types of events, including those generated by the mouse, the keyboard, and various GUI controls, such as a push button, scroll bar, or check box.

Two Event Handling Mechanisms

Before beginning our discussion of event handling, an important point must be made: The way in which events are handled changed significantly between the original version of Java(1.0) and modern version of java, beginning with version 1.1. The 1.0 method of event handling is still supported, but it is not recommended for new programs. Also, many of the methods that supported the old 1.0 event model have been deprecated. The modern approach is the way that events should be handled by all new programs and thus is the method employed by programs.

The delegation Event Model

The modern approach to handling events is based on the delegation event model, which defines standard and consistent mechanisms to generate and process events. Its concept is quite simple: a source generates an event and sends it to one or more listeners. In this scheme, the listener simply waits until it receives an event. Once an event is received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events. A user interface elements is able to “delegate” the processing of an event to a separate piece of code.

Note: Java also allows you to process events without using the delegation event mode. However, the delegation event model is the preferred design for the reason just cited.

Events

In the delegation model, an event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events to be generated are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse. Many other user operations could also be cited as examples.

Events may also occur that are not directly caused by interactions with a user interface For example, an event may be generated when a timer expires, a counter exceeds a value, a software or hardware failure occurs, or an operation is completed. You are free to define events that are appropriate for your application.

Event Sources

A source is an object that generates an event. This occurs when the internal state of that object changes in some way. Sources may generate more than one type of event.

A source must register listeners in order for the listeners to receive notifications about a specific type of event. Each type of event has its own registration method. Here is the general form:

              Public void addTypeListener(TypeListener el)

Here, Type is the name of the event, and el is a reference to the event listener. For example, the method that registers a keyboard event listener is called addKeyListener(). The method that registers a mouse motion listener is called addMouseMotionListener(). When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasting the event. In all cases, notifications are sent only to listeners that register to receive them:

Some sources may allow only one listener to register. The general form of such a method is this:

Public void addTypeListener(TypeListener el)

throws java.util.TooManyListnersException

Here, Type is the name of the event, and el is a reference to the event listener. When such an event occurs, the registered listener is notified. This is known as unicasting the event. A source must also provide a method that allows a listener to unregister an interest in a specific type of event. The general form of such a method is this:

Public void removeTypeListener(TypeListener el)

Here, Type is the name of the event, and el is a reference to the event listener. For example, to remove a keyboard listener, you would call removeKeyListener(). The methods that add or remove listeners are provided by the source that generates events. For example, the component class provides methods to add and remove keyboard and mouse event listeners.

Event Listeners

A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications.

The methods that receive and process events are defined in a set of interfaces found in java.awt.event. For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved. Any object may receive and process one or both of these events if it provides an implementation of this interface. 

READ MORE
...