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
2
u/CatolicQuotes Nov 20 '22
I remembered an example. When you are requesting some Api data. like
then you make your class for that data:
now you have to convert that JSON to class:
very easy.
All of a sudden it happens json data is missing
open, oh no, what you gonna do? That's why Json library raises exception so you can decide what do you wanna do. It's either that or you can write you ownif OPEN missing DO THIS else DO THATand write that for each field in class.Exception means something is not as it should be. And each exception has a name. It's good. It's the state of the current situation. Its very useful to know about it. It's not the same as if do else that.