What is exception handling in C#?
Exception handling is the mechanism used to detect and handle runtime errors in a controlled way. C# primarily uses try, catch, finally, and throw.
C# interview questions covering exceptions, delegates, events & lambdas.
Open a question for the full answer, code and interview guidance.
Exception handling is the mechanism used to detect and handle runtime errors in a controlled way. C# primarily uses try, catch, finally, and throw.
When rethrowing the current exception from a catch block, throw; preserves the original stack trace. Using throw ex; resets important stack-trace information to the current location, making debugging harder.
A custom exception is an application-specific exception type, normally derived from Exception. It can represent a meaningful business or domain failure that built-in exception types do not describe clearly.
A delegate is a type-safe reference to one or more methods with a compatible signature. It allows methods to be passed around as values and invoked indirectly.
Action, Func, and Predicate are built-in generic delegate types.
A lambda expression is a concise way to represent an anonymous function. Lambdas are commonly used with delegates, LINQ, events, and asynchronous APIs.
An anonymous method is an unnamed method defined using the delegate keyword. It was introduced before lambda expressions and can still be used, although lambdas are usually shorter and more idiomatic in modern C#.
An event provides a publisher-subscriber mechanism built on delegates. A publisher exposes an event, and subscribers register handlers that execute when the event is raised.
A delegate is a type-safe method reference that can be assigned and invoked according to its accessibility. An event is a restricted publisher-subscriber abstraction built on a delegate.
A multicast delegate can reference multiple methods. When invoked, the methods in its invocation list are called in order.