C# User-Defined Exceptions - c# - c# tutorial - c# net

How to create user defined exception in C# ?

  • C# allows us to create user-defined or custom exception.
  • It is used to make the meaningful exception.
  • To do this, we need to inherit Exception class.
learn c# tutorials - user defined exception - c# programs

learn c# tutorial -csharp features - user defined exception - c# programs - c# Example

Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# user-defined exception example

using System;  

public class InvalidAgeException : Exception  


public InvalidAgeException(String message)  

: base(message)  




public class TestUserDefinedException  


static void validate(int age)  


if (age < 18)  

throw new InvalidAgeException("Sorry, Age must be greater than 18");  



public static void Main(string[] args)  






catch (InvalidAgeException e) { Console.WriteLine(e); }  
Console.WriteLine("Rest of the code");  


click below button to copy the code. By - c# tutorial - team

C# examples - Output :

InvalidAgeException: Sorry, Age must be greater than 18
Rest of the code

Related Searches to C# User-Defined Exceptions

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.