且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何捕捉异常?

更新时间:2023-11-10 14:11:34

您正在寻找所谓的的 的try-catch 语句:

What you're looking for is called a try-catch statement:

try
{
     var httpResponse = (HttpWebResponse)httpReq.GetResponse();
}
catch (WebException e)
{
    // Here is where you handle the exception.
}

使用 WebException 抓类型语句的意思是特定类型的唯一例外的会被抓住。

Using WebException as the type in the catch statement means only exceptions of that particular type will be caught.

在的情况下发生异常时,电子变量将包含异常的详细信息,如从内这三个异常,内部异常封装的方法传递消息。

In case an exception occurs, the e variable will contain exception details, such as a message passed from the method which three the exception and any inner exceptions encapsulated inside.