Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

The main thing.

When the user clicks the button or presses Enter in the textfield,...

the textfield's String is inspected ...

in our check method.

If the String isn't one of these names,...

our UnknownUserException is thrown.

In this case, our check method is left immediately,...

these lines are skipped,...

and the catch block is executed.

Our check method may throw an UnknownUserException because it has been specified as such.

Our exception class, extending java.lang.Exception.

This constructor is called,...

when we create an instance on the fly, using the keyword new.

In the constructor we call the constructor of the superclass Exception.

The most frequent scenario is, that all goes well, of course,...

so, in this case, no throw takes place,...

the check method continues, executing this line,...

and we return to actionPerformed method, where these lines are executed,....

and the catch block is skipped.

========

The compiler wants us to deal with the exception, so we do this by using a try/catch. A CHECKED exception.

If you want a method where you can choose to ignore the exception, use java.lang.RuntimeException instead of Exception.

========

Here we show the exception object as a string ,...

that contains the string we registered upon construction.

If you want only this error string , you can call
uuex.getMessage( )

in the catch.

========

If you don't want to catch an exception inside a method, you can catch it higher-up :

NumberFormatException can be thrown by Integer.parseInt.

Caught.

========

You can throw different Exceptions in one method :


void someMeth( )
throws UnknownUserException, ExceptionalException, ...
========

If you really need more detail, use this (, without the slashes). Can be seen in the Java Console window, as explained in the Free Environment lesson.

========

NOTES

========

Remove leading and trailing spaces, if any.

========

If you don't want the TextField input to be visible, do this beforehand :
tf.setEchoChar ( '*' ) ;

========

! = NOT && = AND