Solved

Spring MVC Framework Exception Handling (How to create and handle application specific Exception created like MyAppException extends Exception)

Posted on 2008-06-17
5
5,204 Views
Last Modified: 2013-11-24
I am using spring mvc framework. I want to handle my own created Exception (MyException) that extends with java Exception class. What is the best way to handle both application specific Exceptions and any java related exceptions.

Thanks,
Shakil


In my Manager class(i am using DAO and DTO both patterns) i am showing method as below.
public List<TarTrackingResultsDTO> loadTarInfo(long _startingTar,
            String _stateCode) throws AFMException;
 
I want to handle this in controller but there may arise java.lang.Exception related exceptions.
Please give me a peace of code also.

Open in new window

0
Comment
Question by:Shakil_Akhtar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 21816060
I think the best you can do is to catch both exceptions so you do specific things when each one of them is caught:


try
{
   // logic
}
catch (YourException e)
{
    ...
}
catch (Exception ex)
{
    ...
}

Open in new window

0
 
LVL 35

Accepted Solution

by:
girionis earned 125 total points
ID: 21816091
So, if you apply my example above to your code it should be something like



try
{
    loadTarInfo(...);
}
catch (AFMException afm)
{
...
}
catch (Exception e)
{
...
}

Open in new window

0
 

Author Comment

by:Shakil_Akhtar
ID: 21819007
I am doing same as you have given in my example.you haven't given application-servlet.xml entries. should not  give message from super class to which AFMException is derived.like below
try
{
    loadTarInfo(...);
}
catch (AFMException afm)
{
throw new AFMException ("message ");
}
catch (Exception e)
{
...logger.error(" Exception in cotroller");
}
Please give me your comment for confirmation ASAP
0
 
LVL 35

Expert Comment

by:girionis
ID: 21820204
> you haven't given application-servlet.xml entries.

You do not need to declare your exception in xml descriptors.

> Please give me your comment for confirmation ASAP

This should work. But if you are just catching and rethrowing the same exception you might as well declare it in th ethrows clause of your method




public void myMethod() throws AFMException
{
    loadTarInfo(...);
    ...
}

Open in new window

0
 

Author Comment

by:Shakil_Akhtar
ID: 21843368
I am giving u my application-servlet.xml entries  and please respond me ASAP. I want to display two different page. one for afm exception and other for any java.lang.Exception . AFMExceptions are those that is comming from database (like if record unmatch) etc.

<bean id="exceptionResolver" class="com.fareis.afm.common.controller.ExceptionHandler">
<property name="exceptionMappings">
 <props>
<prop key="com.fareis.afm.common.exception.AFMException">afmException</prop>
		<prop key="java.lang.Exception">error</prop> 			</props>
		</property>
	</bean>

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question