Results 1 to 9 of 9
  1. #1
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default 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?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    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                   
    
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.

  7. #7
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    I think you are right, Tolls.

    I tried a very simple program like this:

    Java Code:
    public class Main {
        public static void main(String[] args) {
            Integer n = null;
            int test = n+1;
            System.out.println("test");
        }
    }
    You don't get the string printed out, as soon as the exception is thrown the program terminates.

    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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.

  9. #9
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    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:

    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();
                    }
                }
            });
        }
    2. Around the main method in the Main class:

    Java Code:
            try {
                java.awt.EventQueue.invokeLater(new Runnable() {
                    @Override public void run() {
                        new FmMain().setVisible(true);
                    }
                });
            }
            catch(Exception ex) {
                ex.printStackTrace();
            }
    3. Around the constructor for the form that is instantiated first:

    Java Code:
        public FmMain() {
            try {
                initComponents();
                PnConfig.GetFolders(this);
            }
            catch(Exception ex) {
                ex.printStackTrace();
            }
        }

Similar Threads

  1. How to handle socket Exception
    By mayank0512 in forum Networking
    Replies: 14
    Last Post: 12-21-2010, 11:31 PM
  2. Replies: 5
    Last Post: 08-26-2009, 11:37 AM
  3. Handle exception manually
    By John_28 in forum New To Java
    Replies: 2
    Last Post: 06-05-2008, 11:26 AM
  4. [SOLVED] Handle own exception
    By stevemcc in forum New To Java
    Replies: 3
    Last Post: 04-10-2008, 04:55 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •