简体   繁体   中英

How hide exception while bean initializing

I'm creating some Spring service class. One method returns

com.mxgraph.view.mxGraph

that loads some resources bundle in static context, in particular for current locale, but there is no Russian locale in sources and I'm not going to add them because they're useless in project. I want to handle this class creation and hide exception

java.util.MissingResourceException.

I've tried to write custom bean post processor ( because debugger says it happens in this stage ), but id doesn't work.

And this is the service interface

public interface GraphService
{
    String saveGraph( mxGraphModel graphModel );

    void updateGraph( String id , mxGraphModel graphModel );

    mxGraph getGraph( String id );

    mxGraphModel xmlToGraph( String xml ) throws IOException, SAXException;

    String graphToXml( mxGraph mxGraph );
}

This is the log:

java.util.MissingResourceException: Can't find bundle for base name com.mxgraph.resources.graph, locale ru_RU
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1573)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1396)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:782)
    at com.mxgraph.util.mxResources.add(mxResources.java:55)
    at com.mxgraph.view.mxGraph.<clinit>(mxGraph.java:191)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.sun.proxy.$Proxy578.<clinit>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:739)
    at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:121)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:447)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:333)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:293)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)

I want to hide it just because it's useless exception.

As @Nikolas said: the best way to "hide" an exception is to actually fix it!

But if you really want to hide some exception, it's preferable that you do not, you can use Thread.UncaughtExceptionHandler doing something like that:

public class TestThread implements Runnable {
    @Override
    public void run() {
        throw new RuntimeException();
    }
}

public class ThreadDemo {
    public void startTest() {
        Thread testThread = new Thread(new TestThread());
        testThread.setUncaughtExceptionHandler((t, e) -> {
            // Commenting this line the exception will be not notified
            System.out.println(t + " throws exception: " + e);

            // or filter the exceptions using reflection
            // in your case MissingResourceException.class
            if (e.getClass() == RuntimeException.class) {
                System.out.println("Exception filtered successfully");
            }
        });

        testThread.start();
    }
}

public class Main {
    public static void main(String[] args) {
        ThreadDemo testThread = new ThreadDemo();
        testThread.startTest();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM