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

Custom exception classes in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom exception classes
Define class inheriting Exception
Add constructors or properties
Throw custom exception
Catch custom exception
Handle or display error message
End
Create a new exception type by inheriting from Exception, then throw and catch it like built-in exceptions.
Execution Sample
C Sharp (C#)
class MyException : Exception {
  public MyException(string message) : base(message) {}
}

throw new MyException("Oops!");
Defines a custom exception class and throws it with a message.
Execution Table
StepActionEvaluationResult
1Define class MyException inheriting ExceptionClass createdMyException ready to use
2Create new MyException with message "Oops!"Calls constructorException object with message "Oops!"
3Throw MyExceptionException thrownProgram looks for catch block
4Catch MyExceptionCatch block matches typeException caught and handled
5Display exception messageAccess Message propertyOutput: "Oops!"
💡 Exception caught and handled, program continues or ends gracefully
Variable Tracker
VariableStartAfter Step 2After Step 3Final
exceptionInstancenullMyException("Oops!")ThrownCaught and message accessed
Key Moments - 3 Insights
Why do we inherit from Exception to create a custom exception?
Because inheriting Exception lets our class behave like a real exception, so it can be thrown and caught as shown in execution_table step 1 and 3.
What happens if we throw the custom exception but don't catch it?
The program will crash or stop with an error because no catch block handles it, unlike step 4 where the exception is caught.
How do we pass a message to the custom exception?
We pass the message string to the constructor when creating the exception instance, as in step 2, which stores it for later use.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 2?
AException thrown
BException object with message "Oops!"
CClass created
DException caught and handled
💡 Hint
Check the 'Result' column for step 2 in execution_table
At which step does the program catch the custom exception?
AStep 4
BStep 1
CStep 3
DStep 5
💡 Hint
Look for the 'Catch MyException' action in execution_table
If we do not inherit from Exception, what would happen when throwing the custom class?
AIt would throw normally
BIt would not be caught by catch blocks for Exception
CIt would cause a compile error
DIt would print the message automatically
💡 Hint
Custom exceptions must inherit Exception to be caught as exceptions (see concept_flow)
Concept Snapshot
Create a custom exception by inheriting Exception class.
Add constructors to pass messages.
Throw with 'throw new YourException()'.
Catch with 'catch (YourException e){}'.
Use Message property to show error info.
Full Transcript
This example shows how to create a custom exception class in C# by inheriting from the Exception base class. First, we define the class with a constructor that accepts a message string. Then, we create an instance of this custom exception and throw it. The program looks for a matching catch block to handle the exception. When caught, we can access the message property to display the error. This process allows us to create meaningful, specific errors for our programs that behave like built-in exceptions.