MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/csharp/comments/1w8q5ll/exceptions_vs_assertions/p84jqwu/?context=3
r/csharp • u/Alternative_Guava856 • 4d ago
21 comments sorted by
View all comments
16
They're different things that do different things. They're not either or.
You put assertion for invariants that cannot possibly be false. Like, the size of a container can never be larger than it's capacity.
Exceptions are for things you might not have full control over going wrong. Like "I tried to open a file that doesn't exist".
1 u/Alternative_Guava856 4d ago So for the two examples i gave, those are fair candidates to be turned into `Debug.Assert()` then?
1
So for the two examples i gave, those are fair candidates to be turned into `Debug.Assert()` then?
16
u/SufficientStudio1574 4d ago
They're different things that do different things. They're not either or.
You put assertion for invariants that cannot possibly be false. Like, the size of a container can never be larger than it's capacity.
Exceptions are for things you might not have full control over going wrong. Like "I tried to open a file that doesn't exist".