Hello everyone,
I am trying to release windowing resource after a try-catch and found
that calling System.exit(0) inside finally may hide the an uncaught
exception. See example below.
What is the *right way* to use finally in this case?
--lc
PS.
(1) Problem with putting System.exit(0) at the end is that the JFrame is not
properly released when an uncaught exception (e.g. divide by zero) is
generated.
(2) Now if I move System.exit(0) inside finally clause, then JFrame is
released OK but the uncaught exception is not even reported!
import javax.swing.*;
public class FinallyTest extends JFrame {
FinallyTest(String title) {
super(title);
JLabel l = new JLabel("Calculates Quotient of 2 Numbers....");
getContentPane().add(l);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,100);
setVisible(true);
}
public static void main( String args[] )
{
String s1, s2;
int number1, number2, quotient;
JFrame f = new FinallyTest("Test Finally Clause");
try {
s1 = JOptionPane.showInputDialog( "Enter integer numerator" );
s2 = JOptionPane.showInputDialog( "Enter integer denominator" );
number1 = Integer.parseInt( s1 );
number2 = Integer.parseInt( s2 );
quotient = number1 / number2;
JOptionPane.showMessageDialog(null, "The quotient is " + quotient,
"Results", JOptionPane.PLAIN_MESSAGE );
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid data! Bye!", "Invalid
data", JOptionPane.ERROR_MESSAGE);
}
/*
catch (ArithmeticException e) {
JOptionPane.showMessageDialog(null, "Exception: " + e + "! Bye!",
"Arithmetic Exception", JOptionPane.ERROR_MESSAGE);
}
*/
finally{
// System.exit(0);
}
System.exit(0); // Should do this in the finally clause???
}
}
A. Bolmarcich - 25 Feb 2004 00:10 GMT
> I am trying to release windowing resource after a try-catch and found
> that calling System.exit(0) inside finally may hide the an uncaught
> exception. See example below.
>
> What is the *right way* to use finally in this case?
[helpful example program snipped]
You are using finally in the right way. It is up to you catch the
exceptions before the finally block. One approach you can use is
to add the following catch clause to your try statement
catch (Exception e) {
Thread currentThread = Thread.currentThread();
currentThread.getThreadGroup().uncaughtException(currentThread, e);
}
The error output stream of the java process will contain the usual
lines that are written for an uncaught exception.
Laura P - 26 Feb 2004 08:19 GMT
> I am trying to release windowing resource after a try-catch and found
> that calling System.exit(0) inside finally may hide the an uncaught
[quoted text clipped - 10 lines]
> (2) Now if I move System.exit(0) inside finally clause, then JFrame is
> released OK but the uncaught exception is not even reported!
The finally block is always called: if there is no exceptions it is
called after the try block has finished; if there is a handled
exception the code will leave the try block, execute what is in the
appropriate catch, then go on to the finally; if an uncaught exception
is thrown the code will immediately leave the try block, then execute
whatever is in the finally block, before abandoning the rest of the
method. In your code this has the effect of quitting your program
before any uncaught errors can be reported.
If you want to ensure that any exception thrown in your try block is
handled in some way, catch a general java.lang.Exception, putting this
after the handling blocks for expected possible exceptions.
Laura