Java Exceptions - Handling exceptions without try catch

前端 未结 4 1769
予麋鹿
予麋鹿 2020-12-24 03:35

In Java, we handle exceptions using try catch blocks. I know that I can write a try catch block like the one below to catch any exception thrown in a method.



        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 04:10

    try {
       // do something
       methodWithException();
    }
    catch (Throwable t) {
       showMessage(t);
    }
    
    }//end business method
    
    private void showMessage(Throwable t){
      /* logging the stacktrace of exception
       * if it's a web application, you can handle the message in an Object: es in Struts you can use ActionError
       * if it's a desktop app, you can show a popup
       * etc., etc.
       */
    }
    

提交回复
热议问题