简体   繁体   English

异常处理

[英]Exception Handling

Is there a way in C# to catch any kind of exception? C#中是否可以捕获任何类型的异常? Like in C++ to catch any kind of exception the format is like 就像在C ++中捕获任何形式的异常一样

try{
//Statements
}
catch(...){
// Some more statements
}

But this format in c# fails. 但是,这种格式在C#中失败。 Help? 救命?

You can catch anything like : 您可以捕获到类似以下内容的内容:

catch {}

From .NET 2 and further, this is equivalent to: 从.NET 2开始,进一步等效于:

catch(Exception ex) {}

Because every exception (even a Windows SEH exception) is guaranteed to be derived from System.Exception . 因为每个异常(甚至是Windows SEH异常)都保证从System.Exception派生。

Check this link out. 检查链接。 It's all about exceptions. 都是关于异常的。

What you are trying to do is use a parameter-less catch like this: 您尝试做的是使用无参数捕获,如下所示:

try {
   // your code
} catch {
   // any exception
}
try {
    // Statements
} catch (Exception ex) {
    // Do stuff with ex
}

That should work. 那应该工作。

catch(Exception ex) 抓住(例外)

or catch() <-- i believe the second one works 或catch()<-我相信第二个作品有效

The .NET framework provides a mechanism to detect/handle run time errors. .NET框架提供了一种检测/处理运行时错误的机制。 C# uses three keywords in exception handling: try , catch , finally . C#在异常处理中使用三个关键字: trycatchfinally The try block contains the statement that can cause an exception. try块包含可能导致异常的语句。 The catch block handles the exception, and the finally block is used for cleaning up. catch块处理异常,而finally块用于清除。

try
{
//statements that can cause an exception
}
catch(Type x)
{
//statements for handling an exception
}
finally
{
//cleanup code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM