Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create user defined exceptions in C#?
In C#, you can create user-defined exceptions by deriving from the Exception class or one of its derived classes. User-defined exceptions allow you to create specific error types that are meaningful to your application domain, making error handling more precise and informative.
Syntax
Following is the basic syntax for creating a user-defined exception class −
public class CustomExceptionName : Exception {
public CustomExceptionName() : base() { }
public CustomExceptionName(string message) : base(message) { }
public CustomExceptionName(string message, Exception innerException)
: base(message, innerException) { }
}
To throw a user-defined exception −
throw new CustomExceptionName("Error message");
Key Rules for User-Defined Exceptions
Exception class names should end with "Exception" by convention.
Always provide multiple constructors including parameterless, message-only, and message with inner exception.
User-defined exceptions should be
publicandserializable.Derive from
Exceptionor a more specific exception class likeApplicationException.
Example
Here's an example demonstrating a custom temperature exception −
using System;
public class TempIsZeroException : Exception {
public TempIsZeroException() : base() { }
public TempIsZeroException(string message) : base(message) { }
public TempIsZeroException(string message, Exception innerException)
: base(message, innerException) { }
}
public class Temperature {
int temperature = 0;
public void showTemp() {
if(temperature == 0) {
throw new TempIsZeroException("Zero Temperature found");
} else {
Console.WriteLine("Temperature: {0}", temperature);
}
}
public void setTemp(int temp) {
temperature = temp;
}
}
class TestTemperature {
static void Main(string[] args) {
Temperature temp = new Temperature();
try {
temp.showTemp();
} catch(TempIsZeroException e) {
Console.WriteLine("TempIsZeroException: {0}", e.Message);
}
Console.WriteLine("Setting temperature to 25...");
temp.setTemp(25);
temp.showTemp();
}
}
The output of the above code is −
TempIsZeroException: Zero Temperature found Setting temperature to 25... Temperature: 25
Using Multiple Custom Exceptions
You can create multiple user-defined exceptions for different error scenarios −
using System;
public class InvalidAgeException : Exception {
public InvalidAgeException(string message) : base(message) { }
}
public class NegativeAgeException : Exception {
public NegativeAgeException(string message) : base(message) { }
}
public class Person {
private int age;
public void SetAge(int age) {
if (age < 0) {
throw new NegativeAgeException("Age cannot be negative");
}
if (age > 150) {
throw new InvalidAgeException("Age seems unrealistic");
}
this.age = age;
Console.WriteLine("Age set to: " + age);
}
}
class Program {
static void Main(string[] args) {
Person person = new Person();
try {
person.SetAge(-5);
} catch (NegativeAgeException e) {
Console.WriteLine("NegativeAgeException: " + e.Message);
}
try {
person.SetAge(200);
} catch (InvalidAgeException e) {
Console.WriteLine("InvalidAgeException: " + e.Message);
}
person.SetAge(25);
}
}
The output of the above code is −
NegativeAgeException: Age cannot be negative InvalidAgeException: Age seems unrealistic Age set to: 25
Conclusion
User-defined exceptions in C# provide a way to create specific error types for your application by inheriting from the Exception class. They make error handling more precise and help identify specific problems in your code, improving debugging and maintenance.
