When should Throwable be used instead of new Exception?

前端 未结 12 1241
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 08:21

Given: Throwable is Exception\'s superclass.

When I read texts on writing your own \'exceptions\', I see examples of Throwable

12条回答
  •  旧巷少年郎
    2020-11-28 09:04

    Only two places you should see the word Throwable in code:

    public static void main(String args[])
    {
         try
         {
             // Do some stuff
         }
         catch(Throwable t)
         {
    
         }
     }
    

    AND

    public class SomeServlet extends HttpServlet
    {
          public void doPost(HttpRequest request, HttpResponse response)
          {
             try
             {
                 // Do some stuff
             }
             catch (Throwable t)
             {
                  // Log
             }
          }
     }
    

提交回复
热议问题