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

  • SEARCH
  • Home
  • 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 9012121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:54:52+00:00 2026-06-16T02:54:52+00:00

Basically, what I want to do is pass a specific Exception to a more

  • 0

Basically, what I want to do is pass a specific Exception to a more general Exception within the same try block. I’ve tried the following and it doesn’t work:

    static bool example(int count = 0)
    {
        try
        {
            work();
        }
        catch (TimeoutException e)
        {
            if (count < 3)
            {
                Console.WriteLine("Caught TimeoutException: {0}", e.Message);
                return example(count + 1);
            }
            else
            {
                throw new Exception(e.Message);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Caught Exception: {0}", e.Message + " rethrown");
            return false;
        }
        return true;
    }

    static void work()
    {
        throw new TimeoutException("test");
    }

I want the TimeoutException to be only handled a certain amount of times before going to a more generic Exception. This is because the TimeoutException has additional information about the exception on a case by case basis. I do not want to duplicate the code for Exception under the else clause of TimeoutException. The reason I want all exceptions to be handled is that there may be other unknown exceptions that are thrown. The nature of the program requires it to not crash so I must account for any other exceptions and log them. How can I implement this?

  • 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-16T02:54:54+00:00Added an answer on June 16, 2026 at 2:54 am

    Here’s my take:

    bool example()
    {
        // Attempt the operation a maximum of three times.
        for (int i = 0; i < 3; i++)
        {
            try
            {
                work();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception {0}", e.Message);
    
                // Fail immediately if this isn't a TimeoutException.
                if (!(e is TimeoutException))
                    return false;
            }
        }
    
        return false;
    }
    

    EDIT

    If you want to actually do something with the TimeoutException, you could change the catch block like so:

    catch (Exception e)
    {
        // As Reed pointed out, you can move this into the if block if you want
        // different messages for the two cases.
        Console.WriteLine("Caught exception {0}", e.Message);
    
        TimeoutException timeoutException = e as TimeoutException;
        if (timeoutException != null)
        {
            // Do stuff with timeout info...
        }
        else
        {
            // Not a timeout error, fail immediately
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically want the same functionality as facebook, twitter and all those other infinite
I basically want to be able to deploy multiple versions of the same EAR
I am basically trying to implement a Strategy pattern, but I want to pass
I am using a table inside a repeater. Basically I want to pass to
I am using Ninject with ASP.NET MVC. Basically, I want to pass the protected
I basically want to make an intent and pass it to a service from
Basically I want to pass a string which contains Spanish text that could be
I want to basically create a animation, and when I pass in an integer
Basicly, I want to write a C++ program and with it, pass messages to
I basically want something like: TextView Button Button . . . TextView TextView SeekBar

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.