かんたんASP.NET - リスト
第8章 エラー処理
リスト 8-1 独自エラーページの例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>エラー発生</title>
</head>
<body>
<div style="text-align: center; color:red;">
<p>システム障害が発生しました。</p>
<p>admin@example.comまでご連絡ください。</p>
</div>
</body>
</html>
↑Top
リスト 8-2 customError要素
<customErrors defaultRedirect="~/ErrorPage.html" />
↑Top
リスト 8-3 customError要素にmode属性追加
<customErrors defaultRedirect="~/ErrorPage.html" mode="On" />
↑Top
リスト 8-4 Try~Catchで囲んだ様子
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
' イベントハンドラの処理
Throw New Exception("テスト用の例外です。")
Catch ex As Exception
End Try
End Sub
protected void Button1_Click(object sender, EventArgs e)
{
try
{
// イベントハンドラの処理
throw new Exception("テスト用の例外です。");
}
catch (Exception ex)
{
}
}
↑Top
リスト 8-5 Catch内の処理
Catch ex As Exception
' ログ出力(本来はファイルなどに出力)
System.Diagnostics.Debug.WriteLine(ex)
' 例外を再Throw
Throw
End Try
catch (Exception ex)
{
// ログ出力(本来はファイルなどに出力)
System.Diagnostics.Debug.WriteLine(ex);
// 例外を再Throw
throw;
}
↑Top
リスト 8-6 Page.Errorイベントハンドラ
Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
' 最後に発生した例外を取得
Dim ex As Exception = Server.GetLastError()
' 例外処理
System.Diagnostics.Debug.WriteLine(ex)
End Sub
protected void Page_Error(object sender, EventArgs e)
{
// 最後に発生した例外を取得
Exception ex = Server.GetLastError();
// 例外処理
System.Diagnostics.Debug.WriteLine(ex);
}
↑Top
リスト 8-7 Application.Errorイベントハンドラ
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' 最後に発生した例外を取得
Dim ex As Exception = Server.GetLastError()
' 例外処理
System.Diagnostics.Debug.WriteLine(ex)
End Sub
void Application_Error(object sender, EventArgs e)
{
// 最後に発生した例外を取得
Exception ex = Server.GetLastError();
// 例外処理
System.Diagnostics.Debug.WriteLine(ex);
}
↑Top
リスト 8-8 Default.aspxファイル
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>アプリケーションページ</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="ErrorButton"
runat="server" Text="例外発生" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>アプリケーションページ</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="ErrorButton" runat="server"
Text="例外発生" onclick="ErrorButton_Click" />
</div>
</form>
</body>
</html>
↑Top
リスト 8-9 Default.aspx.vbファイル
Imports System.Diagnostics
Partial Class _Default
Inherits System.Web.UI.Page
' 例外発生ボタンクリック
Protected Sub ErrorButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ErrorButton.Click
' Try~Catchで例外を処理
Try
Dim a As Integer = Integer.Parse("a")
Catch ex As Exception
Debug.WriteLine("[Try - Catch]")
Debug.WriteLine(ex)
' カスタムエラーページを表示させるため、再Throw
Throw
End Try
End Sub
' ページレベルの集約例外ハンドラ
Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
Dim ex As Exception = Server.GetLastError()
Debug.WriteLine("[Page Level]")
Debug.WriteLine(ex)
End Sub
End Class
using System;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
// 例外発生ボタンクリック
protected void ErrorButton_Click(object sender, EventArgs e)
{
// try~catchで例外を処理
try
{
int a = Int32.Parse("a");
}
catch (Exception ex)
{
Debug.WriteLine("[try - catch]");
Debug.WriteLine(ex);
// カスタムエラーページを表示させるため、再throw
throw;
}
}
// ページレベルの集約例外ハンドラ
protected void Page_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
Debug.WriteLine("[Page Level]");
Debug.WriteLine(ex);
}
}
↑Top
リスト 8-10 ErrorPage.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>エラー発生</title>
</head>
<body>
<h2>システム管理者に連絡してください</h2>
</body>
</html>
↑Top
リスト 8-11 Global.asaxファイル
<%@ Application Language="VB" %>
<%@ Import Namespace="System.Diagnostics" %>
<script RunAt="server">
' アプリケーションレベルの集約例外ハンドラ
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim ex As Exception = Server.GetLastError()
Debug.WriteLine("[Application Level]")
Debug.WriteLine(ex)
End Sub
</script>
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Diagnostics" %>
<script RunAt="server">
// アプリケーションレベルの集約例外ハンドラ
void Application_Error(object sender, EventArgs e)
{
// ハンドルされていないエラーが発生したときに実行するコードです
Exception ex = Server.GetLastError();
Debug.WriteLine("[Application Level]");
Debug.WriteLine(ex);
}
</script>
↑Top
リスト 8-12 web.configファイル
<configuration>
<system.web>
~
<customErrors defaultRedirect="~/ErrorPage.html" mode="On" />
~
</system.web>
</configration>
↑Top