Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName Problem

 
Dylan Stamatopolis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a JSP page working perfectly here: /resin/project/
... and in my code, I load the Oracle and MySQL drivers like so:

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Class.forName("com.mysql.jdbc.Driver").newInstance();

However, I need to implement some of this code into a Servlet as well, located here: /resin/project/WEB-INF/classes/
... and did the same declarations like so:

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Class.forName("com.mysql.jdbc.Driver").newInstance();

... but got the following errors:
unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown:
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();


Yeah, I know... I should be catching that exception... BUT !... just wanted to clarify why this exception is being generated in the first place. Totally confused Ideas ?
 
Dylan Stamatopolis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ps... here is my $CLASSPATH:

/root/resin/ts/lib/jsdk24.jar
/root/resin/ts/WEB-INF/lib/mysql-connector-java-3.0.9-stable-bin.jar
/root/resin/ts/WEB-INF/lib/ojdbc14.jar
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65338
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I know... I should be catching that exception... BUT !... just wanted to clarify why this exception is being generated in the first place.


This is basic Java. If a method that you call declares that it throws an exception, you must either catch it or declare that your method throws it.

Since Class.forName() can throw ClassNotFoundException, you need to deal with it.

Btw, if you do catch it, be sure not to just eat the exception. In a servlet environment you should either let such an error propogate out to the container, or wrap it in a ServletException which propogates out. This way, the container can deal with the error as defined by the web application configuration.
 
Dylan Stamatopolis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear... I need some schooling

So, I had to pretty much catch every possible error from the Class.forName... my catches being:


Does this look correct !?
 
Dylan Stamatopolis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the lame post. the above "Catch" statements did work... but just inquiring if that is what the absolute solution is.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65338
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that you're doing exactly what I suggested not doing, which is "eating" the exception. You print out an error and then go merrily along your way as if nothing happened. Not good.
 
Dylan Stamatopolis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you suggest then !?

This class is running solo, and it will never be instantiated, extended... etc... so, I have no where to throw the exceptions to, and must handle them locally.

For instance, if an "InstantiationException" occurred... what should my lonely Servlet do with the exception other than print out the error ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65338
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my previous response.

Servlets should never just "print out" and dismiss execptions. This is not an error like the user typing in invalid data. This type of exception means that something is severely wrong with your program or setup and your servlet should let the exception propogate outward so that it blows up spectacularly in your face.

By dismissing the exception you prevent any error handling set up in the web.xml file from handling the error in whatever way makes the most sense for the application.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic