Should i catch all the exception which are thrown by a method?

前端 未结 3 1364
天涯浪人
天涯浪人 2021-01-27 09:57
try{
   output.format(\"%d %s %s %.2f%n\", input.nextInt(),
        input.next(),input.next(),input.nextDouble());
} catch(FormatterClosedException formatterClosedExcept         


        
3条回答
  •  盖世英雄少女心
    2021-01-27 10:42

    add another catch with Exception class. That is the parent class.

    catch(Exception e){
    //your message or
         System.out.print(e.getMessage());
    }
    

    You can add catch for every exception that your code throws and in the end add this catch with this Exception parent class. If your exception is not caught by any of the catch then this will caught your exception.

    It will catch all exceptions that is raised by your program.

提交回复
热议问题