Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Why not use ... a plain Exception.

It is forbidden.

https://learn.microsoft.com/en-us/dotnet/standard/exceptions...

> Exception ... None (use a derived class of this exception).

https://learn.microsoft.com/en-us/dotnet/standard/design-gui...

> DO NOT throw System.Exception or System.SystemException.



It’s a guideline not to. It’s not a hard rule or forbidden.


I'm not seeing where it says not to throw InvalidOperationException.


InvalidOperationException means "the object is in an inappropriate state". That does not describe a parse error.

C# conventions for exceptions are admittedly a bit confusing. There are a handful of very specific scenarios where you're supposed to use a built-in exception (most commonly ArgumentException). For everything else, you want to define your own type.


The state of the stream, such that it's pointing to an illegal character, actually does seem to be invalid though. Maybe this is an overly pedantic argument. I've been writing quite a bit of c# for over a decade and have basically been doing this the whole time. I thought that I knew how to use exceptions, but it seems I do not.


If you want to recover from CSV errors you should ideally throw InvalidCSVException.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: