Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Debugging Exception Stack Traces | Safe File Operations and Debugging
/
C# Exceptions and Error Handling Practice

bookDebugging Exception Stack Traces

メニューを表示するにはスワイプしてください

When you run into an exception in your code, the error message that appears in the console often includes a stack trace. A stack trace is a list of method calls that the program was executing when the exception occurred. This list shows you exactly where the error happened, and the path the code took to get there. Learning to read stack traces is a crucial skill for debugging, because they point you directly to the source of a problem.

Note
Definition

A stack trace is a report that shows the sequence of method calls leading up to an exception. It helps you trace the flow of execution and quickly identify where things went wrong.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { try { CauseError(); } catch (Exception ex) { Console.WriteLine("An exception occurred:"); Console.WriteLine(ex.ToString()); } } public static void CauseError() { int number = int.Parse("not a number"); } } }

When you run this code, it throws a FormatException because "not a number" cannot be converted to an integer. The output includes a stack trace, which lists each method that was called before the error happened. You will see something like this:

System.FormatException: Input string was not in a correct format.
   at System.Number.ThrowOverflowOrFormatException()
   at System.Int32.Parse(String s)
   at ConsoleApp.Program.CauseError() in C:\...\Program.cs:line 17
   at ConsoleApp.Program.Main(String[] args) in C:\...\Program.cs:line 10

The stack trace starts with the most recent method call, which is where the exception occurred, and works backwards through the chain of calls that led there. Here, you can see that int.Parse failed, which was called inside CauseError, which was called by Main. The file and line numbers help you jump straight to the problem in your code.

1. What information does a stack trace provide?

2. How can stack traces help you fix bugs?

question mark

What information does a stack trace provide?

すべての正しい答えを選択

question mark

How can stack traces help you fix bugs?

すべての正しい答えを選択

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  3

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 4.  3
some-alt