c# creating 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);
}
}
Also in C#:
- unity random float
- reload scene unity
- fahrenheit to celsius c#
- c# serialize
- c# ?
- C# aspnet how to run a migration
- get enum by index c#
- c# error "The name 'ViewBag' does not exist in the current context"
- c# warning CS0108
- how to create a list in c# unity
- c# post request
- c# list string return concatenate
- unity set sprite transparency
- how can convert string to int csharp
- how to a equall to b in c#
- check if current time is in the morning c#
- c# listview
- change text color wpf
- how to create and trigger a function unity animation events
- how to allow user import image c#
- first sentence letter capital in c#
- button commandfield commandargument pass textbox
- iframe set html content c#
- how to disable device guard or credential guard vmware