3 Replies Latest reply: Dec 26, 2012 5:39 PM by TPD-Opitz RSS

    Cannot trap this exception

    Gen.Java
      Hi all,

      Please check the following code

      JFrame f;
      JLabel l=null;
      
      try {
          l = new JLabel("<html> <font size=> hello world </html>");
      } catch ( Throwable t )
              { System.out.println("cannot trap"); }
      
      f=new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().add(l);
      f.pack();
      f.setVisible(true);
      You may notice that font size is not specified. This displays a runtime exception which I cannot trap. I cannot understand why ?

      Thank you
        • 1. Re: Cannot trap this exception
          rp0428
          >
          You may notice that font size is not specified. This displays a runtime exception which I cannot trap. I cannot understand why ?
          >
          Because it was already trapped and ignored. The JLabel constructor caught the exception, printed out the stacktrace and then returned normally.

          There isn't any exception for you to trap.

          Try this sample code that uses a 'myTest' method. The code in the 'main' method cannot trap the exception raised by the divide by zero because the 'myTest' method traps it, prints out a stacktrace and returns normally.

          As far as the code in 'main' is concerned there was no exception.
          import javax.swing.*;
          
          public class Test2 {
          
              public static String myTest(String myParm) {
                  try {
                      if (1 / 0 == 10) {
                          return "Divide by Zero";
                      }
                  } catch (Exception e) {
                          e.printStackTrace();
                          return "No good!";
                  }
                  return "Return after the try.";
          
              }
              
              public static void main (String args []) {
          
                  String in = "in";
                  String out = null;
          
                  try {
                      out = myTest(in);
                  } catch ( Exception e ) {
                      System.out.println("cannot trap");
                  }
          
                  System.out.println ("Got here without trapping!");
              }
          
          }
          
          run:
          java.lang.ArithmeticException: / by zero
          Got here without trapping!
                  at Test2.myTest(Test2.java:7)
                  at Test2.main(Test2.java:24)
          • 2. Re: Cannot trap this exception
            Gen.Java
            Because it was already trapped and ignored. The JLabel constructor caught the exception, printed out the stacktrace and then returned normally.
            But the JLabel constructor is suppose to throw the exception not catch it. The caller of JLabel constructor needs to know if something get wrong.
            • 3. Re: Cannot trap this exception
              TPD-Opitz
              Gen.Java wrote:
              You may notice that font size is not specified. This displays a runtime exception which I cannot trap. I cannot understand why ?
              The answer is burried in the heads of the JVM programmers...

              But I could imagine that they wanted to be compatible with the standard HTML behavior to silently ignore invalid or unknown tags and attributes. So they log the error but keep the program going on...

              bye
              TPD