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

  • Home
  • SEARCH
  • 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 7842133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:14:57+00:00 2026-06-02T16:14:57+00:00

public class PageNotFoundException : HttpException { public PageNotFoundException() : base(404, HTTP/1.1 404 Not Found)

  • 0
public class PageNotFoundException : HttpException
{
    public PageNotFoundException()
        : base(404, "HTTP/1.1 404 Not Found")
    {

    }
}

The idea is that rather than typing this each time

throw new HttpException(404, "HTTP/1.1 404 Not Found") 

I’d rather write

throw new PageNotFoundException();

I was going to add an overload for including the innerException however I will never use this in a try/catch block.

Would you consider this good practice?
i.e. Inheriting from an exception and passing hardcoded information to base(…).

  • 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-06-02T16:14:58+00:00Added an answer on June 2, 2026 at 4:14 pm

    I decided to rewrite my answer to be specific to your actual question, and in a more broad sense that an MVC application isn’t the only thing these best-practices apply to.

    (1) Answer. This is not good practice. You should use a exception builder method instead that throws HttpException directly.

    public static void ThrowPageNotFoundException() {
        throw new HttpException((Int32)HttpStatusCode.NotFound, "HTTP/1.1 404 Not Found");
    }
    

    (2) DO. Use exception builder methods (eg. the code I provided). This allows for you to avoid the extra performance cost of having your own exception type, and allows for it to be inlined. Members throwing exceptions do not get inlined. This would be the proper substitute for convenience throwing.

    (3) DO. Use base class library exceptions whenever possible, and only create a custom exception when there is absolutely no base exception that meets the needed requirements. Creating custom exceptions adds deeper exception hierarchy, which makes debugging harder when it does not need to be, adds extra performance overhead, and also adds extra bloat to your code base.

    (4) Do NOT. Throw the base class System.Exception. Use a specific exception type instead.

    (5) Do NOT. Create custom exceptions for convenience. This is not a good reason for a custom exception, because exceptions are intrinsically costly.

    (6) Do NOT. Create custom exceptions just to have your own exception type.

    (7) Do NOT. Throw exceptions that can be avoided by changing the calling code. This would suggest that you have a usability error in the API rather than an actual problem.

    Anyone who has read Framework Design Guidelines from the .NET development series will know these practices, and they are very good practices. These are the very practices that the .NET framework was built upon, and MVC as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class A { //some fields (may not only primitive types) and methods here}
public class Base{ protected String str; public static final Base ERROR = new Base(error);
public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class Boards : TabActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Tab);
public class BaseFoo { private string param; public BaseFoo(string param) { this.param = param;
public class Example { public Example duplicate; public void duplicateState() { this.example = this.clone();
public class TextBoxDerived : System.Web.UI.WebControls.TextBox { protected override void OnLoad(EventArgs e) { this.Controls.Add(new LiteralControl(Hello));
public class PhotoList : ObservableCollection<ImageFile> { public PhotoList() { } **//this is the line
public class Test { //Car c = null; // Is this better and is
public class Options { public FolderOption FolderOption { set; get; } public Options() {

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.