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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:03:50+00:00 2026-06-04T07:03:50+00:00

How can I throw exception gracefully? public void Test() { if (error != 0)

  • 0

How can I throw exception gracefully?

public void Test() 
{
 if (error != 0) {
  string msg = "Error";
  throw new Exception(msg);
 }

  // Other function
  ...
}

I have also change the throw new Exception(msg); with logger

public void Test()
{
 if (error != 0) {
  string msg = "Error";
  //throw new Exception(msg);
  logger.Error(msg);
 }

  // Other function
  ...
}

Should I use Exit function to exit the function when error hit?

Thnak you.

  • 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-04T07:03:50+00:00Added an answer on June 4, 2026 at 7:03 am

    You want to log before you throw the exception.

    You also want to throw an exception type that inherits from System.Exception so consumers can catch specific types.

    Throwing the exception will exit the function (actually will process a finally block first if you have one) so you only need to throw.

    Also, if you’re catching a different exception in an error condition, you can log and then simply call throw; to throw the original exception and not trash the stack. If you want to throw your own
    exception type in that case, you can include the original exception as the inner exception

    catch (Exception e)
    {
        // log exception details
        throw;
    }
    

    or …

    catch (Exception e)
    {
        // log exception details
        throw new MyCustomException("message", e);   // inherits from Exception
    }
    

    The benefit of the last one (if applicable) is the consumer can catch MyCustomException if it’s interesting for special handling.

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

Sidebar

Related Questions

As Error and Exception are subclass of throwable class, we can throw any error,
Can the new operator throw an exception in real life? And if so, do
I am getting null-exception throw error in the following code. What can be the
Can I throw an exception on the SQL 2005 server so that I can
I noticed that you can't throw an exception in a destructor. So my question
if ConvertAll throw an exception on one element, can i just skip this element
In C#, I can use the throw; statement to rethrow an exception while preserving
Example: @try { // 1) do bad stuff that can throw an exception... //
I have always been of the belief that if a method can throw an
I have the following public interface public interface Bar{ public void DoStuff(); } with

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.