How to Throw a Custom Exception Without the Exception Class in C#
This tutorial will teach you to throw a custom exception in C# without using the Exception
class. First, let’s talk about the concept of an Exception.
Exceptions in C#
A program may encounter an exception at any time during execution. A common definition of an error is an occurrence that causes distress but was not expected.
However, for various reasons, exceptions within an application’s code are par for the course. Exception handling is the logic applications use to handle exceptions whenever they arise explicitly.
An exception may be warranted in a variety of situations. This error can be caused by several different things, including the well-known NullReferenceException
and a database query timeout.
Throw a Custom Exception Without the Exception Class in C#
Custom exceptions are handy if you want to catch and process a specific exception type. They can also help you track critical exceptions.
A unique exception type helps an error-monitoring tool track application problems and logs.
Example
In the below example, we’ll create the EmployeeNotFoundException
class in an application, which will check the employee. It will throw an exception if it is not present in the employee array.
To begin, we’ve to import the following libraries:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Firstly, we’ll create a MyEmployee
class which will hold the employee id and employee name.
class MyEmployee {
public int empid { get; set; }
public string empname { get; set; }
}
Now, we’ll create an EmployeeNotFoundException
type exception. It will be thrown if an employee is not found in the employee array:
class EmployeeNotFoundException : Exception {
public EmployeeNotFoundException(string employeename)
: base(String.Format("Employee Not Found: {0}", employeename)) {}
}
In the Main()
method, we’ll create two MyEmployee
objects named:
MyEmployee emp1 = null;
MyEmployee emp2 = null;
We’ll populate emp1.empname
and emp2.empname
with the employee name and pass the objects to the CheckEmployee
method to check whether the employee exists in the list or not:
try {
emp1 = new MyEmployee();
emp1.empname = "Muhammad Zeeshan";
CheckEmployee(emp1);
emp2 = new MyEmployee();
emp2.empname = "Hermes Fang";
CheckEmployee(emp2);
}
If the employee does not exist in the array, it will throw an EmployeeNotFoundException
.
catch (EmployeeNotFoundException ex) {
Console.WriteLine(ex.Message);
}
Lastly, we’ll create the CheckEmployee()
function, which will validate the employee with the help of .contains()
, as shown below:
public static void CheckEmployee(MyEmployee e) {
string[] arr = { "Muhammad Zeeshan", "Ali Khan", "Saad Jaan" };
if (!arr.Contains(e.empname))
throw new EmployeeNotFoundException(e.empname);
}
Complete Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomxceptionbyZeeshan {
class MyEmployee {
public int empid { get; set; }
public string empname { get; set; }
}
class EmployeeNotFoundException : Exception {
public EmployeeNotFoundException(string employeename)
: base(String.Format("Employee Not Found: {0}", employeename)) {}
}
class Program {
static void Main(string[] args) {
MyEmployee emp1 = null;
MyEmployee emp2 = null;
try {
emp1 = new MyEmployee();
emp1.empname = "Muhammad Zeeshan";
CheckEmployee(emp1);
emp2 = new MyEmployee();
emp2.empname = "Hermes Fang";
CheckEmployee(emp2);
} catch (EmployeeNotFoundException ex) {
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
public static void CheckEmployee(MyEmployee e) {
string[] arr = { "Muhammad Zeeshan", "Ali Khan", "Saad Jaan" };
if (!arr.Contains(e.empname))
throw new EmployeeNotFoundException(e.empname);
}
}
}
Output:
Employee Not Found: Hermes Fang
I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.
LinkedIn