如何在C#中报告异常?

S.P*_*S.P 6 c# throws

我有这样的代码Java,我用来报告异常(抛出FileNotFoundException,IOException,ClassNotFoundException).

例:

private void functionName() throws FileNotFoundException, IOException, ClassNotFoundException{}
Run Code Online (Sandbox Code Playgroud)

我需要这样做C#,我该怎么做?

The*_*ulo 1

这很简单。在C#中,不能直接使用throws语句,因为没有\xc2\xb4t。您可能想使用此代码:

\n\n
    private void functionName(){ throw new IOException();}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这会引发 IOException。由于 IOException 是一个类,因此您需要创建一个新的类,并带有new语句。

\n