How to use Java-style throws keyword in C#?

前端 未结 10 2159
说谎
说谎 2020-11-27 04:39

In Java, the throws keyword allows for a method to declare that it will not handle an exception on its own, but rather throw it to the calling method.

Is there a sim

10条回答
  •  自闭症患者
    2020-11-27 05:20

    The op is asking about the C# equivalent of Java's throws clause - not the throw keyword. This is used in method signatures in Java to indicate a checked exception can be thrown.

    In C#, there is no direct equivalent of a Java checked exception. C# has no equivalent method signature clause.

    // Java - need to have throws clause if IOException not handled
    public void readFile() throws java.io.IOException {
      ...not explicitly handling java.io.IOException...
    }
    

    translates to

    // C# - no equivalent of throws clause exceptions are unchecked
    public void ReadFile() 
    {
      ...not explicitly handling System.IO.IOException...
    }
    

提交回复
热议问题