• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Global Exception Handler In Spring

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Spring mvc based web app where in DAO layer many try and catch blocks are there. We guys are planning to do some DB logging whenever any exception occurred.  I found there is a Global Exception Handler(@ExceptionHandler) in Spring using which I can get rid of applying DB loggers in various catch blocks but unfortunately  Global Exception Handler do not work as every time exception occurs its handled by existing catch block . is there any way to by pass existing catch blocks and use the global one ?
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually not. Once you catch an exception, it's considered handled unless you re-throw it. However, you could try using AOP to inject an exception handler in your Spring components. You should be careful though, to not catch the same exception more than once, e.g. because it goes through multiple components until you finally catch it in your code.
 
Bartender
Posts: 1108
19
Mac OS X IntelliJ IDE Oracle Spring VI Editor 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
You should also be aware that Spring takes checked exceptions from the underling technology and converts them into unchecked.  Arguably this is "good" design as some would say that nobody ever really dealt with checked exceptions anyway.  Of course some will disagree, but that's what spring does.  
Also I believe that most vendor specific database (JDBC) exceptions are converted into springs own exceptions which are common across the vendors.      
 
Marshal
Posts: 77173
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Rooke wrote:. . . that's what spring does.

Whenever you write a framework, you have to take decisions, and that is one of the decisions the creators of Spring took.

Also I believe that most vendor specific database (JDBC) exceptions are converted into springs own exceptions which are common across the vendors.      

I can see how useful it would be to have reproducible error messages independent of the database implementation you are using.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic