0
0
C Sharp (C#)programming~5 mins

Custom exception classes in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom exception class in C#?
A custom exception class is a user-defined class that inherits from <code>Exception</code> or its subclasses to represent specific error conditions in your program.
Click to reveal answer
beginner
How do you define a basic custom exception class in C#?
You create a class that inherits from <code>Exception</code> and provide constructors, for example:<br><pre>public class MyException : Exception { public MyException(string message) : base(message) {} }</pre>
Click to reveal answer
intermediate
Why should you create custom exception classes instead of using general exceptions?
Custom exceptions help you clearly identify and handle specific error cases, making your code easier to read, debug, and maintain.
Click to reveal answer
beginner
Which base class do all custom exceptions in C# usually inherit from?
They usually inherit from the <code>Exception</code> class, which is the base class for all exceptions in C#.
Click to reveal answer
beginner
What is the purpose of calling base(message) in a custom exception constructor?
It passes the error message to the base <code>Exception</code> class so that the message can be accessed later when the exception is caught.
Click to reveal answer
Which keyword is used to create a custom exception class in C#?
Atry
Bexception
Cthrow
Dclass
What should a custom exception class inherit from?
ASystem.Exception
BSystem.Console
CSystem.String
DSystem.Object
Why do you call base(message) in a custom exception constructor?
ATo catch the exception
BTo initialize the base Exception with the error message
CTo throw the exception immediately
DTo log the error message
Which of these is a benefit of using custom exceptions?
AThey automatically fix bugs
BThey prevent exceptions from occurring
CThey make error handling more specific and clear
DThey replace the need for try-catch blocks
Which constructor is commonly included in a custom exception class?
AA constructor that takes a string message
BA constructor that returns void
CA constructor that takes an int
DA constructor that takes no parameters and does nothing
Explain how to create a custom exception class in C# and why it is useful.
Think about class inheritance and error clarity.
You got /4 concepts.
    Describe the role of the base Exception class when defining custom exceptions.
    Base class is the foundation for all exceptions.
    You got /4 concepts.