Why do you have to write throws exception in a class definition?

前端 未结 9 1155
逝去的感伤
逝去的感伤 2020-12-31 09:04

Coming from C#, I just don\'t get this \'throws exception\' that is written after a class/method definition:

public void Test() throws Exception
         


        
9条回答
  •  迷失自我
    2020-12-31 09:57

    Basically yes, and if you don't your program won't compile. This is called a checked exception (Any exception is a checked exception unless it is or extends RuntimeException or Error).

    If there is anything in the method which throws an Exception it won't compile unless you either catch the exception or declare it on the method, and then anything calling that method will have to handle the Exception in the same manner.

提交回复
热议问题