Exception handling and error in general

M

mazdotnet

Hi all,

I'm pretty new to jsp. I have a class set up that connects to the
database and returns the result if successul. I call it from my .jsp
which works fine. My question is lets say I force it to cause an
exception by chaging the word microsoft in the following statement
'Class.forName("com.microsoftssss.sqlserver.jdbc.SQLServerDriver"); .
How do I show that exception in my jsp since it's been thrown in my
class and being caught in the class as well too. What's the best way
of doing this?

Thanks
Maz
 
Joined
Nov 25, 2008
Messages
17
Reaction score
0
Oh!the microsoft jdbc driver is default:com.microsoft.sqlserver.jdbc.S QLServerDriver or com.microsoft.jdbc.sqlserver.S QLServerDriver.if you write wrong words ,you can define you own Exception, and throw it any time or anywhere! depend this ,you can find you error is where !
 
L

Lew

mazdotnet said:
I'm pretty new to jsp (*). I have a class set up that connects to the
database and returns the result if successul. I call it from my .jsp
which works fine. My question is lets say I force it to cause an
exception by chaging the word microsoft in the following statement
'Class.forName("com.microsoftssss.sqlserver.jdbc.SQLServerDriver"); .

Incidentally, you only need to load a JDBC driver once per program run.
How do I show that exception in my jsp since it's been thrown in my
class and being caught in the class as well too. What's the best way
of doing this?

There are several ways, and which one is best depends on the situation. You
say you caught, and presumably handled?, your exception in the logic class.
You probably don't want to show the exception itself to the user - that would
make for a less pleasant user experience. You should likely create a marker
for the error condition, or generate an error message that would be friendly
to the target audience, put that into the program context somewhere and have
the JSP retrieve the message (or use the marker to retrieve a message) for
display.

You should read up on Model-View-Controller (MVC) architectures, in particular
for web apps. Sun describes a version they call "Model 2" on their Java site,
and the Apache Struts library embodies this concept. Java Server Faces (JSF)
embody a more sophisticated form of MVC. It sounds like you have done this,
given your description of the separation of the JSP from the database-access
class.
 
J

J. Davidson

Lew said:
You probably don't want to show the exception itself to the user
- that would make for a less pleasant user experience. You should
likely create a marker for the error condition, or generate an error
message that would be friendly to the target audience

A friendly error message. Yah, that'll be the day.

- jenny
 
A

Arne Vajhøj

mazdotnet said:
I'm pretty new to jsp. I have a class set up that connects to the
database and returns the result if successul. I call it from my .jsp
which works fine. My question is lets say I force it to cause an
exception by chaging the word microsoft in the following statement
'Class.forName("com.microsoftssss.sqlserver.jdbc.SQLServerDriver"); .
How do I show that exception in my jsp since it's been thrown in my
class and being caught in the class as well too. What's the best way
of doing this?

You can use try catch in a JSP page, so if your class does not catch
it then the JSP page can.

Probably better is to use the model where the request goes to
a servlet, which calls a class and the class accesses the database
and forward to two different JSP pages depending on whether
everything went well or an error happened.

Arne
 
A

Arne Vajhøj

Arne said:
It can be argued that errors are never friendly.

"I am extremely sorry that I had to crash and you lost
2 hours of work - be assured that I very much hope that
I will not crash again - you adoring app."

Somehow doesn't cut it.

:)

Arne
 
A

Arne Vajhøj

Lew said:
But their messages can be.

Is it any friendlier not to inform the user of the error?

No.

But I think you are being more rational than users generally are.

Arne
 
V

vk02720

You can use try catch in a JSP page, so if your class does not catch
it then the JSP page can.

Probably better is to use the model where the request goes to
a servlet, which calls a class and the class accesses the database
and forward to two different JSP pages depending on whether
everything went well or an error happened.

Arne

Using MVC/Model 2 is a better choice and the data base error example
in your case should be handled in the Model layer and message passed
to view which should forward to appropriate exception page. However,
in case you have to, you can handle gracefully in jsp also. JSP
provides a facility of error page with following directive - put this
in your main jsp :
<%@ page errorPage="showException.jsp" %>
This would invoke the above jsp error page if your main jsp has an
exception. The error page would have access to exception implicit
variable object which you can then get info out of and display in
error page.
Put this in showException.jsp

<%@ page isErrorPage="true"%>
...
<%-- Exception --%>
<font color="red">
<%= exception.toString() %><br>
</font>

You can always implement this even if you have MVC so Runtime
Exceptions (which is not good idea to "catch" in your normal Java
code) show up in the exception page.

There are other ways also to declare error page in web.xml. See
http://java.sun.com/developer/EJTechTips/2003/tt0114.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top