السلام عليكم ورحمه الله وبركاته
سنقوم بعمل Custome Exception
نفرض اننى امتلك كلاس سيبحث فى قاعدة البيانات عن معلومات تخص أحد الموظفين بأستخدام ID وفى حالة ان يكون الاى دى غير موجود سنقوم بعمل throw EmployeeNotFoundException
- هذا الاكسبشن يرث من ApplicationException
- فى الConstructor سيتم ارسال ال Message
- سنقوم بعمل Hide او Override للخاصية Message الخاصة بال Base Class
class EmployeeNotFoundException:ApplicationException
{
string message;
public new string Message { get { return message; } }
public EmployeeNotFoundException(string Ex)
{
message = Ex;
}
} class Employees
{
public void Search(/*Paramter ID*/)
{
//select Employee Information From Employee Table Where EmployeeId = ID
//If It not Found Throw EmployeeNotFoundException
throw new EmployeeNotFoundException("Employee ID Not Found Please Try Another valid ID");
}
} private void button12_Click(object sender, EventArgs e)
{
try
{
Employees em = new Employees(); em.Search(/*Paramter ID*/);
}
catch (EmployeeNotFoundException ex)
{
MessageBox.Show(ex.Message);
}
}فى آمان الله