I am facing difficulty to handle my own exception from Anonymous Inner class which is a part of Action listener.
Given below is My own Exception:
class Register extends Exception
{
String Error;
public Register()
{
Error = new String("Register Exception");
}
}
Here the method given below is in another class in mouse listener.
public void Gui() throws Register
{
jButton.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
throw new Register(); //Unhandled exception type Register
}
});
throw new Register(); // This works fine.
}
And I am able to work if I throw the exception in method.
There are two types of exceptions in Java: Checked (i.e., you are forced to catch them) and unchecked (you are not forced to catch them). Subclasses of
RuntimeExceptionandErrorare unchecked while subclasses of other exceptions are considered checked. To mitigate the problem in your code change the lineto