Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for c# quicky. Should we 'throw;' or 'throw e;'?
Nick Raphael
Nick Raphael

Posted on

     

c# quicky. Should we 'throw;' or 'throw e;'?

This is a bit of an oldie. Very old, in fact, since this question has been around since c# was born. And likely before that for other languages.

What's the difference between these code snippets?

try{...somecode}catch(Exception){throw;}try{...somecode}catch(Exceptione){throwe;}
Enter fullscreen modeExit fullscreen mode

Well, it's all about where the exception originates and what happend to the stacktrace...

throw : Using a "throw" statement preserves the original error stack information. In exception handling "throw" with empty parameter is also called re-throwing the last exception.

throw e : Using a "throw e" statement, the stack trace of exception will be replaced with a stack trace starting at the re-throw point. It is used to intentionally hide stack trace information.

Most of the time you will want to 'throw;'.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
bellonedavide profile image
Davide Bellone
Fullstack in my past, backend in my future. C# and .NET in my 💘 Content creator, Microsoft MVP, public speaker.
  • Location
    Turin, Italy
  • Work
    Software developer
  • Joined

I've seen many mid-level devs doing the wrong thing. It's a small change that can really help while debugging and analyzing the logs!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Brought to you by Nick Raphael. Fullstack azure architect and tech lead. Currently neck deep in dotnet core - azure stack - microservices.
  • Location
    Sydney, Australia
  • Work
    Team lead, solutions architect at IXUP, also dreamweaver.
  • Joined

More fromNick Raphael

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp