Results 1 to 9 of 9
- 04-22-2010, 05:10 PM #1
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
How does the JVM handle an exception (e.g. NullPointerException) ?
Can anyone explain to me, or point me in the direction of a web page which shows, how the JVM deals with an exception?
If there is a call stack, and a method causes this exception at the end, what happens to the thread that is executing? What, if anything is returned from the methods?
- 04-22-2010, 05:13 PM #2
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 04-22-2010, 05:32 PM #3
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
I don't understand this. It says: "If the runtime system exhaustively searches all the methods on the call stack without finding an appropriate exception handler, as shown in the next figure, the runtime system (and, consequently, the program) terminates.". Well there is no exception handling in my program and it does not terminate when there is a null pointer exception.
- 04-22-2010, 05:47 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-22-2010, 06:54 PM #5
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
I knocked up a test rig in Netbeans so there is some auto-generated code in there. If you press the Tom and Dick buttons you get the numbers printed out. If you press the Harry button it generates an exception but the program does not stop. You can carry on pressing the other buttons.
Java Code:package errorhandlingtest; /** * * @author richardp */ public class NewJFrame extends javax.swing.JFrame { public static String testing; /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { btTom = new javax.swing.JButton(); btDick = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); btTest = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btTom.setText("Tom"); btTom.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btTomActionPerformed(evt); } }); btDick.setText("Dick"); btDick.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btDickActionPerformed(evt); } }); jButton1.setText("Harry"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); btTest.setText("Test"); btTest.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btTestActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btDick, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btTom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 251, Short.MAX_VALUE) .addComponent(btTest) .addGap(27, 27, 27)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btTom) .addComponent(btTest)) .addGap(18, 18, 18) .addComponent(btDick) .addGap(18, 18, 18) .addComponent(jButton1) .addContainerGap(184, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void btTomActionPerformed(java.awt.event.ActionEvent evt) { String test = Test(1); testing = test; System.out.println("* "+test); } private void btDickActionPerformed(java.awt.event.ActionEvent evt) { String test = Test(2); testing = test; System.out.println("** "+test); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String test = Test(null); testing = test; System.out.println("*** "+test); System.out.println("@"); } private void btTestActionPerformed(java.awt.event.ActionEvent evt) { System.out.println("& "+testing); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } public static String Test(Integer i) { System.out.println("> "+i); int j = i+1; return Integer.toString(j); } // Variables declaration - do not modify private javax.swing.JButton btDick; private javax.swing.JButton btTest; private javax.swing.JButton btTom; private javax.swing.JButton jButton1; // End of variables declaration }
- 04-23-2010, 10:00 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
That's because you're using Swing. Swing (and servlet containers) do not kill the thread that throws the uncaught exception, preferring to try and keep the app alive. Users don't like it when their app suddenly vanishes...and you really don't want your whole servlet container going down simply because of a code bug.
- 04-23-2010, 02:34 PM #7
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
I think you are right, Tolls.
I tried a very simple program like this:
You don't get the string printed out, as soon as the exception is thrown the program terminates.Java Code:public class Main { public static void main(String[] args) { Integer n = null; int test = n+1; System.out.println("test"); } }
So, if you don't mind I have two further questions. I am not sure if it would be better to start a new topic, they are related.
1) Does anyone know exactly how Swing handles the NullPointerException?
2) Is there any way I can log what exceptions are thrown? I don't want to get my beta test user to run the program from inside Netbeans! Also, its not feasible for me to put try..catch blocks around EVERYTHING that might cause a null pointer exception. My program has 58000 lines.
- 04-23-2010, 03:07 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You'd usually ensure that it gets caught some where near the top of your app..how you do that with a Swing thing I'd also be intrigued to know. It's been a long time since I've done Swing.
- 04-23-2010, 03:47 PM #9
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
Well I am intrigued as well. I've tried using some simple exception handling code which just prints out a trace in three different places. Neither worked, the exceptions were not caught.
1. Inside the run() method in the Main class:
2. Around the main method in the Main class:Java Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { try { new FmMain().setVisible(true); } catch(Exception ex) { ex.printStackTrace(); } } }); }
3. Around the constructor for the form that is instantiated first:Java Code:try { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new FmMain().setVisible(true); } }); } catch(Exception ex) { ex.printStackTrace(); }
Java Code:public FmMain() { try { initComponents(); PnConfig.GetFolders(this); } catch(Exception ex) { ex.printStackTrace(); } }
Similar Threads
-
How to handle socket Exception
By mayank0512 in forum NetworkingReplies: 14Last Post: 12-21-2010, 11:31 PM -
help debugging: exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By freqrush in forum AWT / SwingReplies: 5Last Post: 08-26-2009, 11:37 AM -
Handle exception manually
By John_28 in forum New To JavaReplies: 2Last Post: 06-05-2008, 11:26 AM -
[SOLVED] Handle own exception
By stevemcc in forum New To JavaReplies: 3Last Post: 04-10-2008, 04:55 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks