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 9250507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:27:36+00:00 2026-06-18T10:27:36+00:00

Why is it adviced most of the time that we should not trap errors

  • 0

Why is it adviced most of the time that we should not trap errors like “Exception” but trap errors that we expect as developers.
Is there a performance hit in trapping generic errors or is it recommended from a best practice point of view?

  try
  {
        // Do something
  }
  catch(Exception e)
  {
        //Log error
  }
  • 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-18T10:27:37+00:00Added an answer on June 18, 2026 at 10:27 am

    The best practice is to catch specific exception first and then move on to more generic ones.

    Exception Handling (C# Programming Guide)

    Multiple catch blocks with different exception filters can be chained
    together. The catch blocks are evaluated from top to bottom in your
    code, but only one catch block is executed for each exception that is
    thrown. The first catch block that specifies the exact type or a base
    class of the thrown exception is executed. If no catch block specifies
    a matching exception filter, a catch block that does not have a filter
    is selected, if one is present in the statement. It is important to
    position catch blocks with the most specific (that is, the most
    derived) exception types first.

    For your question:

    Why is it adviced most of the time that we should not trap errors like
    “Exception” but trap errors that we expect as developers.

    An example would be to catch NullReferenceException. Its never a better practice to catch NullReferenceException, instead one should always check for object being null before using its instance members. For example in case of string.

    string str = null;
    try
    {
       Console.WriteLine(str.Length)
    }
    catch(NullReferenceException ne)
    {
        //exception handling. 
    }
    

    instead a check should be in place for checking against null.

    if(str != null)
       Console.WriteLine(str.Length);
    

    EDIT:

    I think I got the question wrong, If you are asking which exception should be caught and which shouldn’t then IMO, those exceptions which can be dealt with should be caught and rest should be left in the library so they can bubble up to upper layer where appropriate handling would be done. An example would be Violation of Primary key constraint. If the application is taking input(including primary key) from the user and that date is being inserted into the database, then that exception can be caught and a message can be shown to user “Record already exists” and then let the user enter some different value.

    But if the exception is related to the foreign key constraint (e.g. Some value from the dropdown list is considered invalid foreign key) then that exception should bubble up and a generic exception handler should log it in appropriate place.

    For example in ASP.Net applications, these exception can be logged in Application_Error event and a general error page can be shown to the user.

    EDIT 2:
    For the OP’s comment:

    if at a low level if there would be a performance degeradation in
    catching a generic error inspite of knowing if the error is
    sqlexception

    Even if there is going to be any performance difference it should be negligible. But Catch the specific exception, if you know that the exception is going to be SqlException then catch that.

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

Sidebar

Related Questions

I like the Linux operating system and vim editor, but there are many companies
Good advice from CommonWare and Steve H but it's not as easy to me
I work for a company that runs several websites, most of them made with
I am rapidly coming to the conclusion that this is not feasible, although for
I am building the prototype for an Android app that should result in a
I've been doing history tables for some time now in databases, but never put
I have a python program that does something like this: Read a row from
EDIT: I ran the python profiler and the two most time-consuming things (this is
What do people think are the most important issues when developing an application that
I'm trying to construct some app for time schedules of public transport. Most it

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.