C# Corner
Tech
News
Videos
Forums
Trainings
Books
Events
More
Interviews
Jobs
Live
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Difference between "throw" and "throw ex" in C#
WhatsApp
Sujeet Suman
10y
246
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".
People also reading
Membership not found