AWT - SPLessons

AWT Exceptions and Errors

Chapter 16

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

AWT Exceptions and Errors

AWT Exceptions and Errors

shape Introduction

AWT Exceptions and Errors gives the introduction to Error and Exceptions.

Every time when an error occurs, it is not possible to rectify them, which in turn slows down the application performance. To overcome this problem, exceptions are used. Exceptions are nothing but the expected errors that are tested and found solutions before the program gets executed. AWT Exceptions and Errors chapter gives a clear idea about

  • AWTException
  • IllegalComponentStateException
  • Error

AWTException

shape Description

The AWTException occurs during an exceptional condition. AWT classes cannot throw exception, however, the sub-classes of these classes throws the exceptions. For AWT Exceptions that occur during run-time, Exception class is the super class.

shape Example

if (Problem) {
    throw new AWTException ("Problem occurred While Initializing");
}

IllegalComponentStateException

shape Description

IllegalStateException is the super class for this Exception.

IllegalComponentStateException is a run time exception and can be thrown only in 3 positions.

  1. When setCaretPosition() is called before the existence of component’s peer.
  2. When getLoacle() is called without its existence in the container.
  3. When getLocationOnScreen() is called without having any components on the screen.

Using these is not illegal. Instead, these methods should be called after the completion of a particular task.

shape Conceptual
figure

shape Example

import java.awt.TextField;
public class Splesson {
    public static void main (String[] args) {
        new TextField().setCaretPosition (24);
        System.out.println ("Sorry!! Never gets output");
    }
}

Output

Error

shape Description

Error is the serious case to be noted while working with AWT. The AWTError is the sub class of Error class and occur at run-time. If these errors are not caught, then the compiler terminates the program. This functionality can be used in AWT sub-classes and does not require any class file for generation. There is no need of methods as it automatically directs to the top level of the system.

shape Examples

import java.awt.Toolkit;
public class Splesson {
    public static void main (String[] args) {
        System.out.println (Toolkit.getDefaultToolkit());
        System.out.println ("Sorry!! Problem Occurred");
    }
}

Output

Summary

shape Key Points

  • Exceptions occur at run-time and caught by error handling methods.
  • Java Exception class is the super class to all the AWTExceptions.
  • Exceptions provide the messages giving the information of the exceptions occurred.
  • Java Error class is the super class to all the AWTErrors.

shape Programming
Tips

Critical errors must be shown to the user. However, aborting application is not required because terminating a single function skips the remaining functions to work.