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

前端 未结 9 1192
逝去的感伤
逝去的感伤 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:46

    In C++ ( I can't C# but it is probably similar ) keyword throw is a restriction for all of exception types so if there is no specification about throwing exceptions you can suppose that it can be whatever.

    In Java you can suppose if there is no usage of keyword throws then this method throws no one. ( you can be sure, but there needn't be runtime exceptions ). So if there is some specification about throwing you have to catch it of propagate it up

提交回复
热议问题