Hacker News new | past | comments | ask | show | jobs | submit login

When you catch the root Exception class in C# you end up catching IndexOutOfRangeException as well. You should let the program crash instead because this happened due to a bug in your program. Continuing as if nothing happened is unsafe.

Another issue is that you are not allowing higher layers see the exception, even though they may have logic for recovering from the exception. To see the exception they have to now fix your code by removing the catch-all.




Catch the root Exception class, show an error, but let the user continue working if possible. For a web app, there already is a catch-all exception handler at the request level that prevents the entire server from crashing. For other environments, having a catch-all handler at some top-level interaction point (i.e. on buttons or menu items) would be a good choice too.


The “if it’s possible” is a major part of the reason to not catch the root exception.

Catching typed exceptions give you much more easily parsable details about whether or not “it’s possible” to recover.

It’s bad practice to catch the root exception. More often than not, it points to an inexperienced developer


If the exception that was thrown (by a method you called) is in fact a condition your code can recover from, then the functionality of your code was needlessly aborted.


I meant at the higher layers, minus some potential cleaning, a lot of library code is exception neutral and shouldn’t care




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

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

Search: