• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Global Exception Handler In Spring

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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: 21836
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 1046
19
Mac OS X IntelliJ IDE Oracle Spring VI Editor Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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: 66960
255
  • Mark post as helpful
  • send pies
  • 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.
 
Would you like to try a free sample? Today we are featuring tiny ads:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!