What is the base class for all exceptions in C#?

๐Ÿ’ก Concept: Base Exception Class

All exceptions in C# derive from the System.Exception class, which serves as the root for exception handling in .NET.

๐Ÿ“˜ Quick Intro

System.Exception provides properties and methods to handle error information and supports custom exception creation.

๐Ÿง  Analogy

Think of System.Exception as the trunk of a tree, with all specific exceptions branching from it.

๐Ÿ”ง Technical Explanation

  • System.Exception is the base class for all exceptions.
  • Provides properties like Message, StackTrace, and InnerException.
  • Enables catching all exceptions with a general catch block.
  • Custom exceptions inherit from this class.
  • Helps maintain a consistent exception handling framework.

๐ŸŽฏ Use Cases

  • โœ… Catching all exceptions generically.
  • โœ… Extending for custom exception types.
  • โœ… Accessing detailed error information during debugging.
  • โœ… Creating reusable error handling mechanisms.

๐Ÿ’ป Code Example


try {
    // Some risky code
} 
catch (Exception ex) {
    Console.WriteLine(ex.Message);
    Console.WriteLine(ex.StackTrace);
}

โ“ Interview Q&A

Q1: What is the base class for all exceptions in C#?
A: System.Exception.

Q2: What properties does System.Exception provide?
A: Message, StackTrace, InnerException, etc.

Q3: Can you catch all exceptions using System.Exception?
A: Yes, with a generic catch block.

Q4: Why create custom exceptions if System.Exception exists?
A: To represent specific error conditions.

Q5: Is System.Exception serializable?
A: Yes, for remoting and logging.

Q6: What is InnerException?
A: The original exception that caused the current one.

Q7: Can System.Exception be thrown directly?
A: Yes, but generally custom exceptions are preferred.

Q8: How to access stack trace?
A: Using the StackTrace property.

Q9: Are exceptions expensive?
A: They have some performance cost.

Q10: How to best handle exceptions?
A: By catching specific exceptions where possible.

๐Ÿ“ MCQs

Q1. What is the base class for all exceptions?

  • System.Error
  • System.Exception
  • System.Object
  • System.Runtime

Q2. What properties does System.Exception provide?

  • Message only
  • StackTrace only
  • Message, StackTrace
  • None

Q3. Can you catch all exceptions using System.Exception?

  • No
  • Yes
  • Sometimes
  • Never

Q4. Why create custom exceptions?

  • Avoid errors
  • Represent specific errors
  • Ignore errors
  • Crash program

Q5. Is System.Exception serializable?

  • No
  • Yes
  • Maybe
  • Never

Q6. What is InnerException?

  • New exception
  • Original causing exception
  • Random
  • None

Q7. Can System.Exception be thrown directly?

  • No
  • Yes
  • Sometimes
  • Never

Q8. How to access stack trace?

  • Message
  • StackTrace
  • InnerException
  • None

Q9. Are exceptions expensive?

  • No cost
  • Some cost
  • High cost
  • Negligible

Q10. How to handle exceptions best?

  • Catch all
  • Ignore
  • Catch specific exceptions
  • Crash program

๐Ÿ’ก Bonus Insight

Understanding System.Exception is foundational to building effective and maintainable error handling in C# applications.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: