English
Summary
Conclusions
Errors can be syntax errors, runtime errors, or logic errors. Use a debugger to find logic errors: breakpoints (including conditional ones), stepping, and the Locals, Watch, Call Stack, and Immediate windows let you track variable values. Runtime errors in .NET are exceptions — objects of classes derived from Exception. Catch them using catch blocks ordered from specific to general types, refine handling with when filters, and release resources in finally. Methods validate arguments and throw the most specific exception type; the program reports errors through Console.Error and an exit code. Handle expected situations such as invalid input without exceptions, using TryParse and checks.
Self-check questions
- How do syntax, logic, and runtime errors differ?
- What is a breakpoint? How do Step Over, Step Into, and Step Out differ?
- What are the Locals, Autos, Watch, Call Stack, and Immediate windows for?
- How do you configure a conditional breakpoint? What is a tracepoint?
- How does
Debug.WriteLinediffer fromConsole.WriteLine? What doesDebug.Assertcheck? - What is an exception? Which standard exception types do you know?
- How does
try/catchwork? Why doescatchblock order matter? - When does a
finallyblock run? - What are
whenexception filters used for? - How do you throw an exception? Which argument-validation helper methods do you know?
- How do
throw;andthrow ex;differ? - What happens to an unhandled exception? What is a program’s exit code?
- Why is
TryParsepreferable to exceptions for validating input? - How do you create a custom exception class?
- What is the Exception Settings window for?
Useful links
- Visual Studio debugger overview: https://learn.microsoft.com/visualstudio/debugger/debugger-feature-tour
- Breakpoints: https://learn.microsoft.com/visualstudio/debugger/using-breakpoints
- Exceptions in C#: https://learn.microsoft.com/dotnet/csharp/fundamentals/exceptions/
- Exception-handling statements: https://learn.microsoft.com/dotnet/csharp/language-reference/statements/exception-handling-statements
- Exception best practices: https://learn.microsoft.com/dotnet/standard/exceptions/best-practices-for-exceptions
- Managing exceptions in the debugger: https://learn.microsoft.com/visualstudio/debugger/managing-exceptions-with-the-debugger