File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes JSF 2.0 exception handling - Is it necessary to catch the exceptions in the Bean classes ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF 2.0 exception handling - Is it necessary to catch the exceptions in the Bean classes ?" Watch "JSF 2.0 exception handling - Is it necessary to catch the exceptions in the Bean classes ?" New topic
Author

JSF 2.0 exception handling - Is it necessary to catch the exceptions in the Bean classes ?

Mathias Van daele
Greenhorn

Joined: May 29, 2010
Posts: 2
Hello,

I am a newbie in Java JSF. I want to create 1 errorpage for all my different exceptions. (checked)
I am using JSF 2.0.
I throw all the different exceptions from the DAO classes, to the controllers, and then to the bean classes.

In web.xml, I have added the following code :

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorPage.xhtml</location>
</error-page>

If there is an exception, the errorpage is shown correctly.

The problem is, that I don't know how to show the exception message/stacktrace in the errorpage.
Is there an easy way to do this ?
Should I catch or throw the exceptions in the bean class ?

Thanks in advance.






Pradeep Katipamula
Ranch Hand

Joined: May 11, 2010
Posts: 37
Hi Mathias,

Welcome to JavaRanch

Now about your problem, I do not think its a good idea to display the stacktrace to the user. It would be better to show a message that the user understands and log the exceptions for reference.

But if you really want to print the stacktrace. Go through the below post.
stacktrace thread

And in future kindly UseCodeTags for posting any code. Although you have only 4 lines, but in case if you post more code in future this would be better.

Thanks
Pradeep
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Like Pradeep says, displaying a stacktrace in an error page isn't usually recommended. At best, it annoys the user, who won't be able to understand it. At worst, a knowledgeable hacker-type has just learned all sorts of useful information about the internal architecture of your system. Just put up a "user-friendly" message and provide a way for the user to continue on.

On the other hand, DON'T just swallow the stacktrace. Write it to a log file. And I mean a REAL log file, not "system.out/System.err". Most loggers have some sort of method like "log ("Error adding record", exception);" where you can pass the exception to the logger and it will log not only the primary stack trace, but the subsidiary stack traces as well.


Customer surveys are for companies who didn't pay proper attention to begin with.
Mathias Van daele
Greenhorn

Joined: May 29, 2010
Posts: 2
Hello Tim,Pradeep,

Thanks for your answers.

Printing the exception stacktrace in an errorpage, is indeed not a good idea,
but I was just making a web application, for myself, to learn JSF.

Fist I was considering to catch all the exceptions in the bean methods, but this seems like a lot of work.
If I throw them again in the bean methods on the other hand, I don't know how to catch the content/stacktrace of the exception.
I guess there must be a simple solution, but I dont know how.

Thanks again.

Mathias.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF 2.0 exception handling - Is it necessary to catch the exceptions in the Bean classes ?
 
Similar Threads
Handling Exception/Runtime Exception in JSF
JSF : redirect errors to error page
error handling in web.xml
Handling jsf exception
Problems with Exception Handling via web.xml config