Code Monkey home page Code Monkey logo

Comments (5)

Justbeingjustin avatar Justbeingjustin commented on May 18, 2024 1

Maybe something like this could be a good start:

The stack trace is preserved with throw;

Bad

try
{
    // code & logic
}
catch (Exception ex)
{
    throw ex;
}

Good

try
{
    // code & logic
}
catch (Exception ex)
{
    throw;
}

from clean-code-dotnet.

Justbeingjustin avatar Justbeingjustin commented on May 18, 2024

Also, a lot of developer do not understand the difference between throw e; and throw;.

So true!

I remember the day that I read this.

from clean-code-dotnet.

avitsidis avatar avitsidis commented on May 18, 2024

@Justbeingjustin your samples are correct but a second good sample could be whenever you want to throw your own exception:

try
{
    // code & logic
}
catch (Exception ex)
{
    throw new CustomException("some information about the exception", e);
}

Here is a text we could add to a new sub section Rethrowing exceptions in Error Handling section:

Rethrowing exceptions

C# allows you to re-throw an exception in a catch block using the throw keyword. However, it is (most of the time) a bad practice to throw a caught exception using throw e;. This statement will re-throw your exception but it will also reset the stack trace of that exception. If your goal is to re-throw the exact same exception, use throw; so that you keep your stack trace unchanged. If you want to enrich with a custom exception, then you can instantiate a new exception and set its inner exception property with the caught exception with throw new CustomException("some info", e);.

Adding information to an exception is a good practice as it will help when debugging. However if you only want to log or notify an exception happened just use throw; to pass the buck to the caller after you log/notify it.

{samples inserted here}

I'm not an English native, can you tell me if this text is correct technically and grammatically speaking!

If it is correct, I'll do a pull request for this :-]

from clean-code-dotnet.

Justbeingjustin avatar Justbeingjustin commented on May 18, 2024

@avitsidis it appears that there are two issues here.

  1. Resort a few of the entries
  2. Add a new throw entry to the error handling section

Let's move the throw entry conversation to a new issue. #35

from clean-code-dotnet.

thangchung avatar thangchung commented on May 18, 2024

I closed it for now and move to #35

from clean-code-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.