Thursday, August 4, 2022

[FIXED] How to throw exception to next catch?

Issue

enter image description here

I want to throw an exception at next catch, (I attached image)

Anybody know how to do this?


Solution

You can't, and trying to do so suggests that you've got too much logic in your catch blocks, or that you should refactor your method to only do one thing. If you can't redesign it, you'll have to nest your try blocks:

try
{
    try
    {
        ...
    }
    catch (Advantage.Data.Provider.AdsException)
    {
        if (...)
        {
            throw; // Throws to the *containing* catch block
        }
    }
}
catch (Exception e)
{
    ...
}

On the other hand, as of C# 6, there are exception filters so you can check a condition before actually catching the exception:

try
{
    ...
}
catch (Advantage.Data.Provider.AdsException) when (tries < 5)
{
    tries++;
    // etc
}
// This will catch any exception which isn't an AdsException *or* if
// if the condition in the filter isn't met.
catch (Exception e)
{
    ...
}


Answered By - Jon Skeet
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.