public interface ExceptionHandler { void handle(Exception e); } public class MyExceptionHandler implements ExceptionHandler { @Override public void handle(Exception e) { System.out.println("Handling exception: " + e.getMessage()); } } public class UserController { private ExceptionHandler exceptionHandler; public UserController() { String handlerClassName = Config.getProperty("exception.handler"); try { Class<?> handlerClass = Class.forName(handlerClassName); this.exceptionHandler = (ExceptionHandler) handlerClass.newInstance(); e.printStackTrace(); } } @HandleException(type = NullPointerException.class) public void handleNullPointerException() { throw new NullPointerException("Null Pointer Exception"); } public static void main(String[] args) { UserController controller = new UserController(); controller.handleNullPointerException(); } }


上一篇:
下一篇:
切换中文