PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 4, 2022

[FIXED] How to throw exception to next catch?

 August 04, 2022     c#, exception, exception-handling     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing