is it OK to do a nested try/catch?

Posted on 2011-10-07
Last Modified: 2012-05-12

I am getting this error, "Index was outside the bounds of the array"
So I can guess that this is coming out ofthe array/list/ in my case, I think it is coming from the listview.

There is several sections of the code and one try/catch to capture an exception.

Is it ok to insert more try/catch into those small sections of the code to where exactly the error is coming from?
I still this overall try/catch where there is one function from the final{}.

Question by:dkim18
    LVL 4

    Accepted Solution

    Yep, this is always very good practice...
    It helps to control the program flow when unknown situations occur..
    Hope this helps,
    LVL 43

    Assisted Solution

    Yes you can do that, in fact I would say it is good practice.  Gives you tighter control on possible sources of error and what to do in that case.
    LVL 12

    Assisted Solution

    i prefer not to do that because it makes the code more complex by having nested try catches, catching maximum number of exceptions explicitly withing the same try catch would be better. Anyway, here is a good argument on this. See if you get your answer here.
    LVL 16

    Assisted Solution

    There are many ways to code and deal with exceptions. I agree with the others that nesting handlers can often be the best approach, as it allows you to know exactly what failed.

    It never hurts to proactively prevent exceptions or to manually raise one when you detect that an exception WOULD occur.

    For instance, I often add a check before accessing a property or method on an object variable to ensure that the obj is set. Otherwise, you get an unhelpful message like "object not set". I will first check it and if it's null either throw an exception or I'll use a debug.assert.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now