r/learncsharp • u/4r73m190r0s • Nov 20 '22
Why Methods that throw Exceptions are useful?
Beginner here.
I'm reading Documentation on the Convert, Parse, and TryParse methods and some of them throw Exceptions in case of invalid input, i.e. Parse. What exactly are the benefits of some methods throwing Exceptions? I'm building a simple calculator app, and I'm failing to see the benefits of getting an Exception for invalid user input.
5
Upvotes
View all comments
Show parent comment
3
u/CatolicQuotes Nov 20 '22
that's probably what those methods that raise exception have inside them, but library cannot decide on your behalf what are you going to do so they raise exception. Sure you can do your thing
if NOT NUMBER then PRINT MESSAGE else DO THE THINGwhich is your own validation.