I have a web project that has a page called editemployee.aspx. The code behind is editemployee.aspx.cs. From that code, I call another project, a class library. The method I'm calling in that class library looks like...
public static Employee GetItem(int employeeid)
{
try
{
Employee chosenemployee = EmployeeDB.GetItem(employeeid);
chosenemployee.EmployeeInsuranceRecord = EmployeeInsuranceRecordManager.GetItem(employeeid);
return chosenemployee;
}
catch (Exception ex)
{
if (ex is NullReferenceException)
{
//How can I return gracefully from here
//without disrupting the user experience?
}
else
{
throw ex; //if it makes it this far, it must be some other unknown error and I need an exception to be thrown for real so I can track down the bug.
}
}
}
Basically this code gets the full employee record from a data access layer based on the passed EmployeeID. If the exception is NullReferenceException, that means that somehow a bogus EmployeeID got through. In that case, what I want to happen is to return to the calling .aspx.cs page, STOP the process of sorting out the Employee right there, and display a JavaScript alert box that says, "The EmployeeID supplied is not valid."
So I tried saying:
if (ex is NullReferenceException)
{
Response.Write("<script>alert('"The EmployeeID supplied is not valid."')</script>");
}
But this does not work from a separate class library like that, it doesn't recognize Response.Write.
Basically I just need a way to signal, from this method, the calling page to just stop and display an error gracefully, rather than give the user a huge ugly unrecoverable error message. Any tips?