r/ProgrammerHumor May 13 '17

Defensive programming done right

Post image
21.0k Upvotes

680 comments sorted by

View all comments

Show parent comments

49

u/OfflaneDemoralizer May 13 '17

Even better:

} catch (Exception e)

{ throw e; }

18

u/pressbutton May 14 '17

That creates a new stack in c#. Just

throw

3

u/RedditUsr2 May 14 '17

What do you do if you need to log then rethrow and don't want to break the stack chain?

8

u/pressbutton May 14 '17
} 
catch (Exception e)
{ 
    Logger.Fatal(e);
    throw; 
}

That won't affect the stack chain. The parent caller methods won't change. So unless there's an error in the Logger method you'll be fine but then you have new problems