重複の可能性:
再スローによる不適切なスタックトレース
なぜ C# で例外をキャッチして再スローするのかという質問を読みました。の違いについて
try {
// do stuff that might throw an exception
} catch (Exception e) {
throw e; // this destroys the strack trace information!
}
と
try {
// do stuff that might throw an exception
} catch (Exception e) {
throw ; //this preserves the stack trace
}
それから、Main メソッドの ex.StackTrace は、Division メソッドの ex.StackTrace と同じコード行を指していると思います。しかし、これは起こっていません。便宜上、33 行目と 40 行目に印を付けました。
throw
Division メソッドの をに変更すると、throw ex
以下に示すのと同じ出力が表示されます。上記にリンクされている質問では、これら 2 つthrow
の be の違いについて説明していますが、出力に違いは見られません。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExceptionTests
{
class Program
{
static void Main(string[] args)
{
BadMath b = new BadMath();
try
{
int result = b.Division(2, 0);
}
catch (Exception ex)
{
System.Console.WriteLine("\n\nException caught in Main Method");
System.Console.WriteLine(ex.Message);
System.Console.WriteLine(ex.StackTrace);
}
}
}
class BadMath
{
public int Division(int a, int b)
{
try
{
LINE 33 return a / b;
}
catch (DivideByZeroException ex)
{
System.Console.WriteLine("DivideByZeroException caught in Division Method");
System.Console.WriteLine(ex.Message);
System.Console.WriteLine(ex.StackTrace);
LINE 40 throw;
}
}
}
}
出力
DivideByZeroException caught in Division Method
Attempted to divide by zero.
at ExceptionTests.BadMath.Division(Int32 a, Int32 b) in c:\testdev\ExceptionT
ests\ExceptionTests\Program.cs:line 33
Exception caught in Main Method
Attempted to divide by zero.
at ExceptionTests.BadMath.Division(Int32 a, Int32 b) in c:\testdev\ExceptionT
ests\ExceptionTests\Program.cs:line 40
at ExceptionTests.Program.Main(String[] args) in c:\testdev\ExceptionTests\Ex
ceptionTests\Program.cs:line 16
解決
この問題は上記の例では明らかではありませんが、次の例では明らかです。
namespace ExceptionTests
{
class Program
{
static void Main(string[] args)
{
BadMath b = new BadMath();
try
{
int result = b.Division(2, 0);
}
catch (Exception ex)
{
System.Console.WriteLine("\n\nException caught in Main Method");
System.Console.WriteLine(ex.Message);
System.Console.WriteLine(ex.StackTrace);
}
}
}
class BadMath
{
public int Division(int a, int b)
{
try
{
return DoDivision(a, b);
}
catch (DivideByZeroException ex)
{
System.Console.WriteLine("DivideByZeroException caught in Division Method");
System.Console.WriteLine(ex.Message);
System.Console.WriteLine(ex.StackTrace);
throw ;
//throw ex ;
}
}
public int DoDivision(int a, int b)
{
return a / b;
}
}
}
output when using throw
Exception caught in Main Method
Attempted to divide by zero.
at ExceptionTests.BadMath.DoDivision(Int32 a, Int32 b) in c:\testdev\Exceptio
nTests\ExceptionTests\Program.cs:line 47
at ExceptionTests.BadMath.Division(Int32 a, Int32 b) in c:\testdev\ExceptionT
ests\ExceptionTests\Program.cs:line 40
at ExceptionTests.Program.Main(String[] args) in c:\testdev\ExceptionTests\Ex
ceptionTests\Program.cs:line 16
output when using throw ex
Exception caught in Main Method
Attempted to divide by zero.
at ExceptionTests.BadMath.Division(Int32 a, Int32 b) in c:\testdev\ExceptionT
ests\ExceptionTests\Program.cs:line 40
at ExceptionTests.Program.Main(String[] args) in c:\testdev\ExceptionTests\Ex
ceptionTests\Program.cs:line 16