0

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.

4
  • 2
    The best way to "hide" an exception is to actually fix it. Commented Apr 19, 2019 at 8:58
  • Also, post the way you read the properties. Commented Apr 19, 2019 at 8:59
  • @Nikolas, mxGraph library is not my project, in normal way I can’t fix code there. maybe I can add Russian locale properties file, but a) it’s useless, b) we don’t know, how it works, if there are not any properties. And collecting all mandatory values through the code... I guess you understood. That’s why I post question here :) Commented Apr 20, 2019 at 14:29
  • It looked like your custom code. That’s why I was interested to see the way the properties are read. Sometimes, we have to accept the insufficiences of the 3rd party libraries. Have you posted an issue to the project GitHub? Most of the authors read it and try to help :) Good luck Commented Apr 20, 2019 at 14:43

1 Answer 1

0

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.