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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Spring Exception Handling Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (1)
Save
Tweet
89.56K 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

  • API Security Tools: What To Look For
  • JIT Compilation of SQL in NoSQL
  • How To Check for JSON Insecure Deserialization (JID) Attacks With Java
  • The Power of Enum: Make Your Code More Readable and Efficient [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo