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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:44:51+00:00 2026-06-18T06:44:51+00:00

As far as I know, you are supposed to only use try/catch when you

  • 0

As far as I know, you are supposed to only use try/catch when you will actually handle the exception and not just report&log it and then crash the application. Otherwise, you are better off just checking different scenarios where it makes sense (e.g. if sth==null) or – if your purpose is just to log the exception and crash the application – to use AppDomain.UnhandledException. But is this always the case and why?

Suppose the following method, which accepts an array and returns MemoryStream after doing some database and filesystem operations.

MemoryStream Read (int[] IDs)
{
    try
    {
        using (SqlConnection connection = new SqlConnection(connection_string))
        {
            connection.Open();    

                        // a bunch of code executing SQL queries & constructing MemoryStream, which is returned at the end of the block
        }
    }
    catch (Exception e)
    {
        // report the exception to the user & log it
        throw; // pointles??                
    }
}

There are multiple situations that can be considered exceptional/unwanted behavior, such as:

  • argument (IDs[]) being null,
  • failure to establish an SQL connection,
  • failure to execute a specific SQL query.

All these cases are considered exceptional, still putting everything inside a try/catch if you only want to log an exception (then crash) is probably bad practice – but why? What would be the best handling behavior in the above case? Avoid try/catch completely, check null references using an if statement (return null in such case) and use AppDomain.UnhandledException to log everything else? Use try/catch, but still check for null references inside using if statements (and return in that case)? Something else?

  • 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-18T06:44:52+00:00Added an answer on June 18, 2026 at 6:44 am

    you are supposed to only use try/catch when you will actually handle the exception and not just report & log it and then crash the application

    I agree with the first part, although I would add that adding logging at the tier boundaries is valuable when you may not have control over the calling tier. e.g. I log all exceptions that occur in a Web Service at the top method to ensure I have logging on the server since debug info (stack trace, etc) does not always cross comm layers gracefully

    In your particular example I would check for “exceptional” conditions where you can but let other exception occur “naturally”. For your specific examples:

    • argument (IDs[]) being null,
    • failure to establish an SQL connection,
    • failure to execute a specific SQL query.

    For the first one, I would check for null arguments for one reason: A NullReferenceException gives you no context about the cause of the exception, other that where it occurs. I much prefer to check for null and then throw a new ArgumentNullException exception since you can add which argument is null. You may still need to do some digging to find out why it’s null but it saves you a lot of time in debugging.

    SQL Exceptions can typically bubble up naturally, since they have decent error information in them (e.g. "undeclared variable '@arg'")

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

Sidebar

Related Questions

So far I know that FileSystemWatcher can look into a folder and if any
As far as know, I must be careful with PHP, and I think Javascript.
as far as I know both 2012-07-04T17:30:52+00:00 and 2012-07-04T17:30:52Z are dates in iso8601 format
As far as I know there is no way to migrate existing Facebook comments
As far as I know, C2DM is per app per device thing..but my question
As far as I know, Timer runs asynchronously, so If a very short duration
As far as I know that JSF keeps all the session scoped bean in
As far as I know, VisualSVN Server is a server while TortoiseSVN is a
as far as i know there are cross domain restictions that wont allow you
As far as I know de standard folder for Users (usually C:\Users) can be

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.