[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5209
  • Last Modified:

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

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
Shakil_Akhtar
Asked:
Shakil_Akhtar
  • 3
  • 2
1 Solution
 
girionisCommented:
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
 
girionisCommented:
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
 
Shakil_AkhtarAuthor Commented:
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
 
girionisCommented:
> 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
 
Shakil_AkhtarAuthor Commented:
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now