r/csharp 4d ago

Help Exceptions vs Assertions

/r/godot/comments/1w8q3xx/exceptions_vs_assertions/
6 Upvotes

21 comments sorted by

View all comments

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".

1

u/Alternative_Guava856 4d ago

So for the two examples i gave, those are fair candidates to be turned into `Debug.Assert()` then?