DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Exception Handling Tutorial

Spring Exception Handling Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · Tutorial
Like (1)
Save
Tweet
Share
91.37K Views

Join the DZone community and get the full member experience.

Join For Free

you need not handle any database-related exceptions explicitly instead spring jdbc framework will handle it for you. all the exceptions thrown by the spring jdbc framework are subclasses of dataaccessexception which is a type of runtimeexception, so you need not handle it explicitly. any checked exception when thrown will be mapped to any of the subclasses of the dataaccessexception by the framework.

package com.vaannila.dao;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import org.springframework.dao.dataaccessexception;
import org.springframework.dao.duplicatekeyexception;
import org.springframework.dao.emptyresultdataaccessexception;

import com.vaannila.domain.forum;

public class main {

	public static void main(string[] args) {
		applicationcontext context = new classpathxmlapplicationcontext(
				"beans.xml");
		forumdao forumdao = (forumdao) context.getbean("forumdao");
		forum springforum = new forum(1, "spring forum",
				"discuss everything related to spring");
		try {
			forumdao.insertforum(springforum);
		} catch (duplicatekeyexception e) {
			system.out.println("forum already exist");
		} catch (dataaccessexception e) {
			e.printstacktrace();
		}
		try {
			system.out.println(forumdao.selectforum(2));
		} catch (emptyresultdataaccessexception e) {
			system.out.println("the forum id is invalid");
		} catch (dataaccessexception e) {
			e.printstacktrace();
		}

	}

}

don't handle any exceptions in the dao layer, instead throw it to the front-end and handle it. to handle the exception all you need to do is to catch the appropriate exception in the hierarchy and address it. when you run this example for the first time a forum will be created, when you run it again you will get a duplicatekeyexception , which we catch it in the catch block and display the appropriate message. only one forum exist in the database with the forum id ' 1 ', if you query for the forum with forum id ' 2 ' then you will get the " the forum id is invalid " message dispalyed on the console.

the class hierarchy of the duplicatekeyexception is shown below.

you can download and try the example here.

source : download
source + lib : download

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot, Quarkus, or Micronaut?
  • Full Lifecycle API Management Is Dead
  • mTLS Everywere
  • NoSQL vs SQL: What, Where, and How

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: