### Question:
GuiceExceptionResolver implements ExceptionResolver { @Override public void handleException(Throwable t, HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp) { if (t instanceof MethodInvokingException) { t = t.getCause(); } for (ExceptionBind bind : binds) { if (bind.getExceptionClass().isInstance(t)) { bind.getHandler(injector).handleException(t, servlet, req, resp); return; } } defaultHandler.handleException(t, servlet, req, resp); } @Inject  GuiceExceptionResolver(Set<ExceptionBind> exceptionBinds, @Named(ExceptionResolver.DEFAULT_EXCEPTIONHANDLER_NAME) ExceptionHandler defaultHandler); @Override void handleException(Throwable t, HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp);  }### Answer:
@Test public void testHandlingInheritance() { Injector injector = Guice.createInjector(new TestExceptionInheritModule()); ExceptionResolver resolver = injector.getInstance(ExceptionResolver.class); resolver.handleException(new Custom1ArgumentException(), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("SecondCustomHandler:Custom1ArgumentException")); resolver.handleException(new Custom2ArgumentException(), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("SecondCustomHandler:Custom2ArgumentException")); resolver.handleException(new Custom3ArgumentException(), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("SecondCustomHandler:Custom3ArgumentException")); resolver.handleException(new Custom4ArgumentException(), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("SecondCustomHandler:Custom4ArgumentException")); resolver.handleException(new IllegalArgumentException("illegal"), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("DefaulHandler:illegal")); }
@Test public void testHandling() { Injector injector = Guice.createInjector(new TestExceptionModule()); ExceptionResolver resolver = injector.getInstance(ExceptionResolver.class); resolver.handleException(new NullPointerException(), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("FirstCustomHandler")); resolver.handleException(new Custom1ArgumentException(), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("SecondCustomHandler:Custom1ArgumentException")); resolver.handleException(new IllegalArgumentException(), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("SecondCustomHandler:IllegalArgumentException")); resolver.handleException(new RuntimeException("runtime"), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("DefaulHandler:runtime")); }
@Test public void testHandlingMethodException() { Injector injector = Guice.createInjector(new TestExceptionModule()); ExceptionResolver resolver = injector.getInstance(ExceptionResolver.class); resolver.handleException(new MethodInvokingException(null, new NullPointerException()), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("FirstCustomHandler")); resolver.handleException(new MethodInvokingException(null, new Custom1ArgumentException()), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("SecondCustomHandler:Custom1ArgumentException")); resolver.handleException(new MethodInvokingException(null, new IllegalArgumentException()), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("SecondCustomHandler:IllegalArgumentException")); resolver.handleException(new MethodInvokingException(null, new RuntimeException("runtime")), null, null, null); Assert.assertTrue(Output.getInstance().msg.contains("DefaulHandler:runtime")); }