> Good evening all,
>
[quoted text clipped - 6 lines]
> button processes the code it seems like the exception handling needs to be
> there.
It does indeed. You can't change the definition of the ActionListener interface
to make it throw an exception, so you must handle it.
An Exception must be dealt with at some point. If you use a method which is
declared to throw SomeException then you must either handle that exception
within your method by "catch"ing it, or declare your method to throw the same
SomeException so that it can be handles further up the call stack. If you
choose not to handle the Exception in your code the JVM will eventually handle
it for you, but at the expense of your program "crashing". To catch and handle
SomeException you must enclose the code which may throw SomeException in a try
{} block, with an associated catch(SomeException e) {} block to handle what
happens if SomeException is actually thrown.
So, lets suppose that you have methodA declared to throw IOException:
void methodA throws IOException() {}
and you use this method in methodB. You can elect not to handle it yourself, and
let the caller of your method handle it by throwing it:
void methodB throws IOExcetption() {
...
use.methodA();
...
}
or catch it and handle it:
void methodB() {
...
try {
...
use.methodA();
...
}
catch(IOException e) {
// Execution jumps here if IOException is thrown within the try{} block
// Now deal with the Exception, usually do something with e
}
}
> I don't know of another way to call the button other than using the
> listener, that triggers the event when the button is pressed. At this
[quoted text clipped - 17 lines]
>
> {
If this is really the code then this is what is generating the error message.
Besides having to catch or throw an Exception which *is* thrown, you *cannot*
catch or throw an Exception which isn't thrown. Your try block only has one
redundant statement, which can't throw the caught IOException, so the compiler
is telling you that the try/catch block is not valid. Also, don't just ignore
the IOException (the catch block is empty), at least print a stack trace.
> FileWriter swriter = new FileWriter("c:\\studname.txt",true);
> JOptionPane.showMessageDialog(null,"The file could not be written.");
[quoted text clipped - 8 lines]
> //outputFile2.close();
> StudentTextField.setText("");
The above code *can* throw IOException, and this code should be within the try{}
block.

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555