Exceptions
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
When a method cannot do its job, it throws an exception: an object that carries a message and a stack trace. Follow one through a real program: CheckAge(-5) throws an ArgumentException onto the heap, the runtime unwinds CheckAge's frame off the call stack, Main's catch handles it and prints "Age cannot be negative", and the finally block prints "done". Lines that never run are skipped.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01CheckAge(-5) throws on CheckAge line 3. What happens to CheckAge line 5, return age?
throw ends the method immediately. Nothing after it in CheckAge runs; the runtime leaves the method and looks for a catch further up the call stack.
02CheckAge has no try/catch of its own. Where does the exception go?
The runtime searches up the call stack. Each method without a matching catch is left (its frame popped): this is unwinding. Main's try has a catch that matches.
03Which exception types does catch (ArgumentException e) catch?
A catch clause takes its own type and every derived type. FormatException is not derived from ArgumentException, so it would not be caught here.
04You put catch (Exception e) before catch (ArgumentException e) in the same try. What happens?
Catch clauses are tried in order, so a general catch placed first would hide the specific one. C# rejects that with error CS0160. Put the specific types first and catch (Exception) last.
05When is a finally block guaranteed to run?
finally runs on the way out of the try statement, whether it finished normally or an exception was caught. For an exception that nothing catches, Microsoft does not guarantee it.
المسرد
- Exception
- An object of a class derived from System.Exception that reports an error. It carries a Message and a StackTrace.
- throw
- The statement that raises an exception. The current method stops at once and the runtime looks for a handler.
- try / catch
- try marks code that might fail; a catch clause after it handles exceptions of its type or any derived type.
- finally
- A block after try (and any catch) that runs when the try statement ends, normally or after a caught exception: the place for clean-up.
- Call stack
- The list of method calls that have not finished yet. Each call has a frame holding its parameters and locals; the newest is on top.
- Unwinding
- Leaving methods, and popping their frames, while the runtime searches up the call stack for a matching catch.
- Unhandled exception
- An exception that no catch on the stack matches. .NET ends the process and prints its type, message and stack trace.
الملاحظات والمصادر
- Top-level statements run inside a Main method the compiler generates for you, so the bottom frame of the call stack is Main.
- The call stack is drawn simply: one block per call holding its parameters and locals. Real frames also hold the return address and saved registers, and the JIT may keep small values in CPU registers instead.
- The runtime fills in StackTrace as the exception travels up the stack, starting from the method that threw it.
- If no catch anywhere on the stack matches, the exception is unhandled: .NET ends the process and prints "Unhandled exception." with the type, message and stack trace to standard error. For an unhandled exception, Microsoft does not guarantee that finally blocks run.
- The figure simplifies the order of events. .NET handles an exception in two passes: first it searches up the stack for a matching catch (here, in Main), and only then does it unwind, popping CheckAge's frame and running any finally blocks on the way.
- Throwing is much slower than a normal return, so .NET guidance is to use exceptions for errors, not for ordinary control flow; for expected failures such as bad user input, methods like int.TryParse return false instead.
- The program is real C#: put both parts in Program.cs (dotnet new console) and dotnet run prints "Age cannot be negative" and then "done".
- Microsoft Learn: Exceptions and exception handling (C#)
- Microsoft Learn: Exception-handling statements (throw, try-catch, try-finally)
- Microsoft Learn: Creating and throwing exceptions
- Microsoft Learn: Handling and throwing exceptions in .NET
- Microsoft Learn: Best practices for exceptions
- Microsoft Learn: Exception class
- Microsoft Learn: ArgumentException class
- Microsoft Learn: Compiler error CS0160
- Microsoft Learn: Top-level statements
- ECMA-335: Common Language Infrastructure (exception handling, method state)
اختبار يتاح بعد الخطوة الأخيرة.