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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:12:18+00:00 2026-05-25T02:12:18+00:00

I do not really use any try/catches in my code ever but I’m trying

  • 0

I do not really use any try/catches in my code ever but I’m trying to break that habit and now get in to using exceptions.

I figure the most important place to have it in my application would be reading a file and I’m trying to implement that now but I’m unsure of the “best-practices” for doing so. Currently I’m doing something like this:

private void Parse(XDocument xReader)
{
    IEnumerable<XElement> person = xReader.Descendants("Person").Elements();

    foreach (XElement e in person)
        personDic[e.Name.ToString()] = e.Value;

    if (personDic["Name"] == null || personDic["Job"] == null || personDic["HairColor"] == null)
        throw new KeyNotFoundException("Person element not found.");
}

But I am unsure if this is correct. I have this for handling it:

try
{
    personsReader.Read(filename, persons);
}
catch (KeyNotFoundException e)
{
    MessageBox.Show(e.Message);

    return;
}

// Do stuff after reading in the file..

However when showing e.Message it just shows the generic KeyNotFoundException error message and not by custom error message. Also I’m not sure if in general I am going about this whole “exception handling stuff” properly. I do return in the catch because if the file is not read successfully obviously I just want to pretend like the user never tried to open a file and let him try again with another file.

Am I doing this properly? Again I am fairly new to using exceptions and I want to make it sure I got it down right before continuing on and applying this to the rest of my program.

Also, why do people say not to do catch (Exception e)? It seems like in this case I would want to do that because regardless of what error occurs when reading in a file, if there is an error, I want to stop reading the file, display the error message, and return. Wouldn’t that always be the case? I can understand not wanting to handle Exception e if you would want to handle something differently based on the exception but in this case wouldn’t I want to just handle the base exception class in case anything goes wrong?

  • 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-25T02:12:18+00:00Added an answer on May 25, 2026 at 2:12 am

    You should catch exceptions when you can handle the condition and do something useful. Otherwise you should let it bubble up the call stack and perhaps someone above you can handle it. Some apps have unhandled exception handlers to handle it at the outer most layer but in general, unless you know you have some useful way to handle it, let it go.

    In your case, you’re handling not being able to read a resource and informing the user. You’re handling it. Concerning the generic exception, one thing you can do is catch and re-throw a better exception. If you do that, make sure you incude the root cause exception as the inner exception. You can also trace or log the details if appropriate.

    throw new MyGoodExceptionType ("Could not read file", e);  // e is caught inner root cause.
    

    Now the UI shows a good error and perhaps the inner root cause is in a log etc…

    Some typical mistakes:

    • Handling exceptions deep in the stack in a generic library method: Remember that a common library function may get called in many different code paths. You likely don’t have the context whether it should be handled and whether it’s appropriate to handle it. the caller higher in the stack likely has context and knows whether it’s safe to handle. Typically that means higher layers of code decide to handle. In lower layers, typically you let them flow.

    • Swallowing Exception: Some code catches exceptions (especially lower in the stack) and then the root condition just evaporates making it maddening to debug. Once agan, if you can handle it, do so. If not, let it go.

    • Exceptions should be exceptional: Don’t use excpetions for flow control. For example, if you’re reading a resource, don’t try and read and then catch the exception and make a decision point. Instead, call ifexists, check bool and make decisions in your code. this especially helps when you set the debugger to break on exceptions. You should be able to run clean and if the debugger breaks, it should be a real issue. Having the debugger break constantly when debugging is problematic. I personally like throwing exceptions very rarely and always try to avoid for flow control.

    Hope that helps.

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

Sidebar

Related Questions

I know, that I am not the first one to try to use Cocoa
I must honestly say that I do not really understand much about casting, but
I'm not really asking whether I should use either a RDBMS or config files
Strangely enough, this is not on The Google. And I could really use a
Not really a programming question, but relevant to many programmers... Let's say I have
I'm trying to use a case statement to make some code more readable. It
This is what I am trying to achieve but I'm not familiar with the
I have been trying to use protobuf-net with MonoTouch but I have no idea
I am trying something really simple in F# to try and test interaction with
Not really getting the point of the map function. Can anyone explain with examples

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.