Catch the specific exception in C#
Forums:
class IsNullException : Exception { }
class IsEmptyException : Exception { }
static void ThrowWhenNullOrEmpty(List<int> list)
{
if (list == null) {
throw new IsNullException();
}
if (list.Count == 0) {
throw new IsEmptyException();
}
}
try {
var list = new List<int>();
ThrowWhenNullOrEmpty(list);
}
catch (IsNullException e) {
Console.WriteLine("list is not specified");
}
catch (IsEmptyException e) {
Console.WriteLine("list is empty");
}
- Log in to post comments