Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Difference between "throw" and "throw ex" in C#
WhatsApp
Sujeet Suman
9y
241.3
k
0
9
25
Blog
throw :
If we use "throw" statement, it preserve original error stack information. In exception handling "throw" with empty parameter is also called re-throwing the last exception.
throw ex :
If we use "throw ex" statement, 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.
catch
(Exception ex)
{
// do some stuff here
throw
;
// a) continue ex
throw
new
MyException(
"failed"
, ex);
// b) wrap
throw
new
MyException(
"failed"
);
// c) replace
throw
ex;
// d) reset stack-trace
}
So it is good practice to use the "throw" statement, rather than "throw ex" because it will give us more accurate stack information rather than "throw ex".
Up Next
Difference Between Throw And Throw(Ex) In C#
Ebook Download
View all
Programming Dictionary in C#
Read by 29k people
Download Now!
Learn
View all
Membership not found