c# creating and throwing exceptions
class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
}
[Serializable]
class InvalidStudentNameException : Exception
{
public InvalidStudentNameException()
{
}
public InvalidStudentNameException(string name)
: base(String.Format("Invalid Student Name: {0}", name))
{
}
}
class Program
{
static void Main(string[] args)
{
Student newStudent = null;
try
{
newStudent = new Student();
newStudent.StudentName = "James007";
ValidateStudent(newStudent);
}
catch(InvalidStudentNameException ex)
{
Console.WriteLine(ex.Message );
}
Console.ReadKey();
}
private static void ValidateStudent(Student std)
{
Regex regex = new Regex("^[a-zA-Z]+$");
if (!regex.IsMatch(std.StudentName))
throw new InvalidStudentNameException(std.StudentName);
}
}static void CopyObject(SampleClass original)
{
if (original == null)
{
throw new System.ArgumentException("Parameter cannot be null", "original");
}
}
Also in C#:
- Title
- solid principles c#
- Category
- C#
- Title
- bool toggle unity c#
- Category
- C#
- Title
- how to find a transform unity
- Category
- C#
- Title
- how to cast list to observablecollection c#
- Category
- C#
- Title
- how to stop window from terminating c# visual studio
- Category
- C#
- Title
- how to redirect to extern page in .net core
- Category
- C#
- Title
- unity reference parent gameobject
- Category
- C#
- Title
- how to make a C# application visual studio code
- Category
- C#
- Title
- unity instantiate vector3
- Category
- C#
- Title
- c# creating and throwing exceptions
- Category
- C#
- Title
- how to go to other forms in C#
- Category
- C#
- Title
- c# dictionary literal initializer
- Category
- C#
- Title
- c# loop
- Category
- C#
- Title
- c# list tuple
- Category
- C#
- Title
- blazor display validation message
- Category
- C#
- Title
- freeze rotation in code c#
- Category
- C#
- Title
- reload scene unity
- Category
- C#
- Title
- c# param exception
- Category
- C#
- Title
- hwo to make an array in C#
- Category
- C#
- Title
- ignore fakeiteasy
- Category
- C#
- Title
- print line in python
- Category
- C#
- Title
- unity check if gameobject is active
- Category
- C#
- Title
- hot to move pobject unity
- Category
- C#
- Title
- bundle.config in mvc is missing
- Category
- C#