Women in Technology

Hear us Roar



Article:
  Best Practices for Exception Handling
Subject:   Runtime Exception is not informative
Date:   2007-08-16 22:11:24
From:   remotec
Response to: Runtime Exception is not informative

johnydep


I believe the correct way to convert a checked exception into a runtime exception would be the following;


try{
Class.forName("jdbc.odbc");
}catch (ClassNotFoundException ce){
throw new RuntimeException(ce);
}


Note that we are passing the the checked exception into the runtime exception. This will generate the following stack trace.


Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: jdbc.odbc
at ExceptionTest.main(ExceptionTest.java:25)
Caused by: java.lang.ClassNotFoundException: jdbc.odbc
at java.net.URLClassLoader.findClass(URLClassLoader.java:376)
at java.lang.ClassLoader.loadClass(ClassLoader.java:572)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:442)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
at java.lang.Class.forName1(Native Method)
at java.lang.Class.forName(Class.java:180)
at ExceptionTest.main(ExceptionTest.java:23)