Hear us Roar
Article:
 |
|
Best Practices for Exception Handling
|
| Subject: |
|
Runtime Exception is not informative |
| Date: |
|
2007-03-06 22:31:05 |
| From: |
|
johnydep
|
|
|
|
Exceptions have been developed to give more flexibility to the developer while debugging the code
here is sample method. Intention of the code is to raise exception
try{
Class.forName("jdbc.odbc");
}catch (ClassNotFoundException ce){
ce.printStackTrace();
}
following error was displayed
java.lang.ClassNotFoundException: jdbc.odbc at java.net.URLClassLoader.findClass(URLClassLoader.java:241) at java.lang.ClassLoader.loadClass(ClassLoader.java:516) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:460) at java.lang.ClassLoader.loadClass(ClassLoader.java:448) at java.lang.Class.forName1(Native Method) at java.lang.Class.forName(Class.java:142) at test.exechandle.TestAddition.getdatabase(TestAddition.java:29) at test.exechandle.TestExecption.main(TestExecption.java:22)
Now the code was modified to throw the run time exception
try{
Class.forName("jdbc.odbc");
}catch (ClassNotFoundException ce){
throw new RuntimeException();
}
Error
java.lang.RuntimeException at test.exechandle.TestAddition.getdatabase(TestAddition.java:31) at test.exechandle.TestExecption.main(TestExecption.java:22)
Exception in thread "main"
if we compare both the exceptions i would see the complie time exception is more informative rather than runtime.
So is that advisiable that we ommit the complie time exception
|
|
| |