例外を検出する
最終更新日: 2010年3月24日
適用対象: SharePoint Server 2010
例外がスローされる可能性のあるコードのセクションを try ブロックに配置し、例外を処理するコードを catch ブロックに配置します。Catch 文の順序は重要です。例外が発生すると、例外はスタックの上位に渡され、各 catch ブロックは例外を処理する機会が与えられます。例外を処理する catch ブロックは、例外の種類と catch ブロックで指定された例外名を突き合わせて決定されます。たとえば、以下の catch ブロックは Simple Object Access Protocol (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 e As SoapException
Console.WriteLine("SOAP Exception Error Code: {0}", e.SubCode.Code.Name)
Console.WriteLine("SOAP Exception Message is: {0}", e.Message)
End Try
タイプ固有の catch ブロックが存在しない場合、例外は一般的な catch ブロック (ある場合) によって検出されます。たとえば、以下のコードを追加して一般的な例外を検出できます。
catch (Exception e)
{
Console.WriteLine("Exception Message: {0}", e.Message);
}
Catch e As Exception
Console.WriteLine("Exception Message: {0}", e.Message)
End Try
一般的な例外の前に、固有タイプの例外を対象とする catch ブロックを配置します。
共通言語ランタイムは、catch ブロックによって検出されない例外を検出します。ランタイムの構成内容によって、デバッグ ダイアログ ボックスが表示されるか、プログラムの実行が停止して例外情報を示すダイアログ ボックスが表示されます。デバッグの詳細については、「デバッグ、トレース、およびプロファイリング」(https://msdn.microsoft.com/ja-jp/library/7fe0dd2y(vs.71).aspx) を参照してください。
例外の処理方法については、「例外処理の実施」(https://msdn.microsoft.com/ja-jp/library/seyhszts(vs.71).aspx) を参照してください。
例
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();
}
}
}
Imports System
Imports System.Text
Imports System.Web.Services.Protocols
Imports ExcelWebService.myserver02
Namespace ExcelWebService
Friend Class WebService
<STAThread> _
Shared Sub Main(ByVal args() As String)
' Instantiate the Web service and make a status array object
Dim xlservice As New ExcelService()
Dim outStatus() As Status
Dim rangeCoordinates As New RangeCoordinates()
Dim sheetName As String = "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";
Dim targetWorkbookPath As String = "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.
Dim sessionId As String = xlservice.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", 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
Dim rangeResult1() As Object = xlservice.GetRange(sessionId, sheetName, rangeCoordinates, False, outStatus)
Console.WriteLine("Total rows in range: " & rangeResult1.Length)
Console.WriteLine("Sum in last column is: " & (CType(rangeResult1(2), Object()))(3))
' Close the workbook. This also closes the session.
xlservice.CloseWorkbook(sessionId)
Catch e As SoapException
Console.WriteLine("SOAP Exception Error Code: {0}", e.SubCode.Code.Name)
Console.WriteLine("SOAP Exception Message is: {0}", e.Message)
Catch e As Exception
Console.WriteLine("Exception Message: {0}", e.Message)
End Try
Console.ReadLine()
End Sub
End Class
End Namespace
関連項目
タスク
[ウォークスルー] Excel Web Services を使用してカスタム アプリケーションを開発する
[方法] SubCode プロパティを使用してエラー コードをキャプチャする