Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6955717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:46:26+00:00 2026-05-27T14:46:26+00:00

I have a web project that has a page called editemployee.aspx. The code behind

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:46:26+00:00Added an answer on May 27, 2026 at 2:46 pm

    Why don’t you catch your NullReferenceException from your editemployee.aspx.cs?
    Something like:

    try
    {
        var employee = Employee.GetItem(int employeeid)
    }
    catch(NullReferenceException ex)
    {
        Response.Write("<script>alert('"The EmployeeID supplied is not valid."')</script>"); 
    }
    

    Plus, you should not use the is operator to filter exception type but add the correct type in the catch statement.

    A catch block can specify an exception type to catch. This type is called an exception filter, and must either be the Exception type, or derived from this type. Application-defined exceptions should derive from ApplicationException.

    (source)

    Finally, it’s a recommended practice to throw more specified Exception. In your case a custom EmployeeNotFoundException would be better to throw from your business layer.

    catch(NullReferenceException ex)
    {
         throw new EmployeeNotFoundException(ex);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .net project that has a web reference to a service. I
We have a updatable web site project that is written in c#, it has
I have an ASP.NET VB.NET web project that references a VB.NET class library. I
I have a Web Application that I received from another developer, when I first
Within a Web Service CarService, I have a class called Car. Car has a
So I've taken over a VB.net web application project from another developer and have
I have a web project that I developed where one of the main functions
I have a web application project that works with a quite large database (over
I have inherited a web project that is perl based and I'm attempting to
I have a web application project (wap) that is successfully being deployed to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.