1

i have created Standalone application in spring. for exception handling i am using custom exception handler which extends SimpleMappingExceptionResolver class.

whenever exception occurs in program i want to delegate it to specific java method. how do i do that ?

i saw lots of examples on the net, but everywhere exception handling done on .jsp page. how i catch the exception in java method.

here is my bean config file

<bean class="com.ys.core.exception.ExceptionHandler">
<property name="exceptionMappings">
<props>
<prop key="com.ys.core.exception.MyException">ExceptionHandler</prop>
<prop key="java.lang.Exception">ExceptionHandler</prop>
<prop key="java.lang.ArithmeticException">ExceptionHandler</prop>
</props>
</property>
</bean> 

<bean id="exceptionHandler" class="com.ys.core.exception.ExceptionHandler" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/com/ys/core/service/myservices" />
<property name="suffix" value=".java" />
</bean>

can i do like this ? means call .java class instead of jsp file ?

  • What you are describing is Spring's exception handling in the Spring MVC Controllers. In a standalone application you won't have Spring MVC Controllers, and therefore none of that information applies. You will need to roll your own implementation – geoand Jun 04 '14 at 06:52
  • Thanks geoand,your reply was really helpful – user3705792 Jun 04 '14 at 08:41

2 Answers2

0

ExceptionProcessorAspect : It's an after throwing aspect which is handling a Pointcut with annotation : ExceptionAnnotation

In side the @AfterThrowing handler, exception Processor is triggered which is based on the Command Pattern

  @AfterThrowing(pointcut = "getExceptionPointcut()", throwing ="error")
public void processor(JoinPoint call,Throwable error){
    if(null!=call){
        MethodSignature signature = (MethodSignature) call.getSignature();
        Method method = signature.getMethod();
        ExceptionAnnotation myAnnotation = method.getAnnotation(ExceptionAnnotation.class);
        Class<?> value = myAnnotation.value();
        AbstractExceptionProcessor exceptionProcessor = (AbstractExceptionProcessor)this.exceptionMap.get(value);
        exceptionProcessor.processException(error);


    }
}

Other classes are the supporting components that make up an @AfterThrwoing aspect

1.ExceptionAnnotation : a base on which the pointcut is made 2.SystemException : which will be thrown from a TestExceptionClass.testEx1

-1
package com.spring;

public abstract class AbstractExceptionProcessor {





    public void processException(Throwable error){
        System.out.println("Processed "+error);
    }
}
..............................................................................

package com.spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


@Component("com.spring.ExceptionProcessorAspect")
@Aspect
public class ExceptionProcessorAspect {

    //@Value("#{exceptionMap}")
    private Map<Class<?>,AbstractExceptionProcessor> exceptionMap;

    @Autowired
    @Qualifier("com.spring.SystemExceptionProcessor")
    private SystemExceptionProcessor systemExceptionProcessor;

    @Autowired
    @Qualifier("com.spring.UnsupportedExceptionProcessor")
    private UnsupportedExceptionProcessor unsupportedExceptionProcessor;

     public ExceptionProcessorAspect(){

     }


     @PostConstruct
     public void init(){
         this.exceptionMap = new HashMap<Class<?>,AbstractExceptionProcessor>();
         exceptionMap.put(SystemException.class, systemExceptionProcessor);
         exceptionMap.put(UnsupportedException.class, unsupportedExceptionProcessor);
     }
    @Pointcut("@annotation(ExceptionAnnotation)")
    public void getExceptionPointcut(){

    }

    @AfterThrowing(pointcut = "getExceptionPointcut()", throwing ="error")
    public void processor(JoinPoint call,Throwable error){
        if(null!=call){
            MethodSignature signature = (MethodSignature) call.getSignature();
            Method method = signature.getMethod();
            ExceptionAnnotation myAnnotation = method.getAnnotation(ExceptionAnnotation.class);
            Class<?> value = myAnnotation.value();
            AbstractExceptionProcessor exceptionProcessor = (AbstractExceptionProcessor)this.exceptionMap.get(value);
            exceptionProcessor.processException(error);


        }
    }

}
................................................................................................................................................................
package com.spring;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;



@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExceptionAnnotation {

    Class<?> value();


}

................................................................................................................................................................


/**
 * 
 */
package com.spring;


public class SystemException extends RuntimeException {



    public SystemException(String message) {
        super(message);
        this.message = message;
    }

    String message;






}


................................................................................................................................................................



package com.spring;

import org.springframework.stereotype.Component;



@Component("com.spring.SystemExceptionProcessor")
public class SystemExceptionProcessor extends AbstractExceptionProcessor {






    @Override
    public void processException(Throwable error) {
        // TODO Auto-generated method stub
        System.out.println("Processed " + error.getMessage());
    }

}
................................................................................................................................................................
package com.spring;

import org.springframework.stereotype.Component;





@Component
public class TestExceptionClass {




    @ExceptionAnnotation(value=SystemException.class)
    public void testEx1(){
        System.out.println("In SystemException Block" );
        if(1==Integer.parseInt("1")){
            throw new SystemException("SystemException raised");
        }

        System.out.println("After throws Block SystemException");
    }




}
................................................................................................................................................................



/**
 * 
 */
package com.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class BootClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-resources/config.xml");

        System.out.println("Spring context initialized.");



        TestExceptionClass test =  applicationContext.getBean(TestExceptionClass.class);
        test.testEx1();
    }

}
  • 1
    A code dump is not helpful, please explain what your code is doing and how it helps – Tim Dec 09 '15 at 10:07