Tuesday, November 30, 2010

4 tips on exception handling



    Using exceptions for execution control in unexceptional situations is not recommended as it makes reading the code very difficult.






    Creating an exception is expensive because of the initialization in the fillInStackTrace() method.






    Exceptions raised from the JVM itself (e.g. NullPointerException, ClassCastException, ArrayIndexOutOfBoundsException) can be extremely fast as after a while the (HotSpot) virtual machine just returns the same exception object with no stack trace. This would make debugging difficult if you get that.






    You can use the -XX:-OmitStackTraceInFastThrow to turn off the ability of the JVM to throw fast stackless exceptions.






No comments: