如何:捕获异常
将可能引发异常的代码段放在 Try 块中,而将处理异常的代码放在 Catch 块中。Catch 语句的顺序很重要。发生异常时,异常沿堆栈向上传递,每个 Catch 块都有机会处理它。通过将异常类型与 Catch 块中指定的异常名称相匹配,可确定处理异常的 Catch 块。例如,以下 Catch 块将捕获简单对象访问协议 (SOAP) 异常:
catch (SoapException e)
{
Console.WriteLine("SOAP Exception Error Code: {0}",
e.SubCode.Code.Name);
Console.WriteLine("SOAP Exception Message is: {0}",
e.Message);
}
如果不存在特定类型的 Catch 块,则由可能存在的常规 Catch 块来捕捉异常。例如,您可以通过添加以下代码来捕获一般异常:
catch (Exception e)
{
Console.WriteLine("Exception Message: {0}", e.Message);
}
将针对特定类型异常的 Catch 块放在一般异常之前。
公共语言运行库将捕捉 Catch 块未捕捉的异常。根据运行库的配置,可能会显示一个调试对话框,或者停止执行程序并显示一个包含异常信息的对话框。有关调试的详细信息,请参阅Debugging and Profiling Applications(https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondebuggingprofiling.asp)。
有关如何处理异常的详细信息,请参阅Best Practices for Handling Exceptions (https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconbestpracticesforhandlingexceptions.asp)。
示例
using System;
using System.Text;
using System.Web.Services.Protocols;
using ExcelWebService.myserver02;
namespace ExcelWebService
{
class WebService
{
[STAThread]
static void Main(string[] args)
{
// Instantiate the Web service and make a status array object
ExcelService xlservice = new ExcelService();
Status[] outStatus;
RangeCoordinates rangeCoordinates = new RangeCoordinates();
string sheetName = "Sheet1";
// Set the path to the workbook to open.
// TODO: Change the path to the workbook
// to point to a workbook you have access to.
// The workbook must be in a trusted location.
// If workbookPath is a UNC path, the application
// must be on the same computer as the server.
// string targetWorkbookPath =
// @"\\MyServer\myxlfiles\Formulas.xlsx";
string targetWorkbookPath =
"http://myserver02/DocLib/Shared%20Documents/Basic1.xlsx";
// Set credentials for requests
xlservice.Credentials =
System.Net.CredentialCache.DefaultCredentials;
try
{
// Call the OpenWorkbook method and point to the
// trusted location of the workbook to open.
string sessionId = xlservice.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", out outStatus);
Console.WriteLine("sessionID : {0}", sessionId);
// Prepare object to define range coordinates,
// and call the GetRange method.
rangeCoordinates.Column = 0;
rangeCoordinates.Row = 0;
rangeCoordinates.Height = 18;
rangeCoordinates.Width = 10;
object[] rangeResult1 = xlservice.GetRange(sessionId, sheetName, rangeCoordinates, false, out outStatus);
Console.WriteLine("Total rows in range: " + rangeResult1.Length);
Console.WriteLine("Sum in last column is: " + ((object[])rangeResult1[2])[3]);
// Close the workbook. This also closes the session.
xlservice.CloseWorkbook(sessionId);
}
catch (SoapException e)
{
Console.WriteLine("SOAP Exception Error Code: {0}",
e.SubCode.Code.Name);
Console.WriteLine("SOAP Exception Message is: {0}",
e.Message);
}
catch (Exception e)
{
Console.WriteLine("Exception Message: {0}", e.Message);
}
Console.ReadLine();
}
}
}
See Also
任务
演练:使用 Excel Web Services 开发自定义应用程序
概念
其他资源
How to: Capture Status Return Values