Class AWTException in Java

Class AWTException in Java

An AWTException is thrown from an application when an exception has occurred in  Abstract Window Toolkit during execution. An AWTException; thrown to indicate an exceptional condition; must be caught or declared in a throws clause.

AWTException is a subclass of Exception. Any public class in java .awt we do not use it. It is only convenient if we create our custom code to throw an AWT related exception. This exception is thrown if we try to do within an AWT component and that is not correct.

Being a generic exception it is thrown just when an exception condition occurs within AWT environment. When the user of any java.awt class subclasses if the person may throw AWTException just to indicate a problem. However note instead of creating a custom exception and throw it, it is always advisable to throw AWTException.

JVM(Java) ensures that an occurrence of AWTException the program will exit and terminate. Throwing an instance of AWTException is similar like any Exception. It should be caught in a corresponding catch block.

The hierarchy of AWTException

AWTException hierarchy
AWTException hierarchy

Implemented interface

  • Serializable

The class structure of AWTException is given as:

public class java.awt.AWTException extends java.lang.Exception{
//constructor
public AWTException(String message);//constructs an AWTException with the specified detailed message.
} 

AWTException supports one field called serialVersionUID. The declaration is private static final long serialVersionUID.

See also  Class UnsupportedFlavorException in java

The class AWTException also inherits methods from class Object and Throwable.

 

From Object classFrom Throwable class
clone()fillInStackTrace()
getClass()getMessage()
notify()printStackTrace()
wait()printStackTrace(PrintWriter)
wait(long, int)getLocalizedMessage()
wait(long)printStackTrace(PrintStream)
equals(Object)addSuppressed()
finalize()getCause()
hashCode()getSuppressed()
notifyAll()getStackTrace()
hashCode()initCause()
notifyAll()setStackTrace()
toString()

How to throw an AWTException?

AWTException can be used in a similar manner just like any other Throwable object.

If(SomeIssue){
	throw new AWTException(“Issue detected while doing xyz”);
}

Packages that use Java.awt.AWTException are as follows-

  • java.awt- This package is having responsible for creating UI, graphics painting, paint image etc.
  • java.awt.image- This package is responsible for providing interfaces that enable the input methods for the java runtime environment.
  • org.micromanager.internet.utils.ScannerImage- This package is responsible to create image from a region and throws java.awt.AWTException.
  • vo.writeGIF- This package is responsible to create GIF image and may throw java.awt.AWTException.
  • java.awt.Robot: – This package is responsible for simulating the user’s activity via keyboard and mouse input. It can also throw java.awt.AWTException.

Java.awt.AWTException example in java.awt.Robot class

import java.awt.*;
public class TestRobot{
public static void main(String ar[]){
Robot rt=new Robot();
Rt.mouseMove(30,30);
}
}

How to solve AWTException?

Each time an error occurs, it is not a good idea to rectify them as they have a serious ill effect in the throw of application performance. This is the prime reason why we use Exception. An exception is known error. These exceptions are actually tested before and some solution is already available.

    • We need to use try-catch block to catch any Exception
try{
Robot rt= new Robot();
Rt. mouseMove(30,30);
}
Catch(AWTException e) {  }
}
    • declare that this class can throw AWTException

public class TestRobot throws AWTException{………}

Note:
java.awt class can not throw the exception directly but a subclass of AWTException can throw it.

See also  Class NullPointerException in Java - 2 Important Points To Know

The other common mechanism to solve AWTException :

  • The exception that is coming during runtime must be caught by a handling mechanism.
  • Java Exception class is the superclass of all AWTException.
  • Exceptions should provide some meaningful message.
  • Java error class is the superclass of all AWTErrors.
  • The users must be shown any critical error/exception occurred.

 

Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *