This discussion is archived
3 Replies Latest reply: Dec 26, 2012 3:39 PM by TPD-Opitz-Consulting-com RSS

Cannot trap this exception

Gen.Java Newbie
Currently Being Moderated
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 Guru
    Currently Being Moderated
    >
    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 Newbie
    Currently Being Moderated
    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-Consulting-com Expert
    Currently Being Moderated
    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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points