• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.






 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mathias Van daele
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic