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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:31:49+00:00 2026-05-10T16:31:49+00:00

This question is a follow-up from How to indicate that a method was unsuccessful

  • 0

This question is a follow-up from How to indicate that a method was unsuccessful. The xxx() Tryxxx() pattern is something that can be very useful in many libraries. I am wondering what is the best way to offer both implementations without duplicating my code.

What is best:

public int DoSomething(string a) {      // might throw an exception } public bool TrySomething(string a, out result) {     try     {         result = DoSomething(a)         return true;     }     catch (Exception)     {         return false;     } 

or

public int DoSomething(string a) {      int result;      if (TrySomething(a, out result))      {          return result;      }      else      {          throw Exception(); // which exception?      } } public bool TrySomething(string a, out result) {     //... } 

I’d instinctively assume that the first example is more correct (you know exactly which exception happened), but couldn’t the try/catch be too expensive? Is there a way to catch the exception in the second example?

  • 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. 2026-05-10T16:31:49+00:00Added an answer on May 10, 2026 at 4:31 pm

    Making TrySomething just catch and swallow the exception is a really bad idea. Half the point of the TryXXX pattern is to avoid the performance hit of exceptions.

    If you don’t need much information in the exception, you could make the DoSomething method just call TrySomething and throw an exception if it fails. If you need details in the exception, you may need something more elaborate. I haven’t timed where the bulk of the performance hit of exceptions is – if it’s the throwing rather than the creating, you could write a private method which had a similar signature to TrySomething, but which returned an exception or null:

    public int DoSomething(string input) {     int ret;     Exception exception = DoSomethingImpl(input, out ret);     if (exception != null)     {         // Note that you'll lose stack trace accuracy here         throw exception;     }     return ret; }  public bool TrySomething(string input, out int ret) {     Exception exception = DoSomethingImpl(input, out ret);     return exception == null; }  private Exception DoSomethingImpl(string input, out int ret) {     ret = 0;     if (input != 'bad')     {         ret = 5;         return null;     }     else     {         return new ArgumentException('Some details');     } } 

    Time this before you commit to it though!

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

Sidebar

Related Questions

This is a follow on from my previous question although this is about something
This is a follow-on question from the one I asked here . Can constraints
This is a follow up from a question of mine that was just answered
This question is a follow on from the one that was asked here .
As a follow on from this question. How can I call a function and
This question is a follow on from this one ... I am binding to
This is a follow-on from this question, in which I was trying to suppress
This is a follow up question from Calling constructor in return statement . This
This is a follow up from my previous question I have this code basically
This is a follow-on from a previous SO question Anchoring CSS Repeating Background Image

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.