6 ms·
If you reach a scenario that should crash your app, exceptions are fine. However, they are a pain (and brittle in the case of C#) when the intention is to handl
by osigurdson 1mo ago
If you reach a scenario that should crash your app, exceptions are fine. However, they are a pain (and brittle in the case of C#) when the intention is to handle them. This is the reason why many newer languages don't use exceptions.
- Cthulhu_ 1mo agoThey're also a "thinking error"; most errors are not exceptional and should instead be part of the regular flow of your application. But in 90's/2000's languages like Java and C#, they consider "this file does not exist" as an exception. Second issue is that they include a call stack and are generally pretty expensive to construct. To circumvent this, a lot of exception languages are written in a more defensive style.
- throwaway2037 1mo agoIt is interesting that you picked Java/C# and "they consider "this file does not exist" as an exception". Firstly, I know more about Java. It is not an exception when testing for the existance of a file that does not exist. However, it is an exception to attempt to open a file (for reading) that does not exist. Also, for those unaware, exception class FileNotFoundException is a subclass of IOException, which is "checked" and cannot be ignored. However, when calling the C function "open" (or "fopen") to open a file, the programmer can easily forget to check the return value. This is harder to do in Java, thanks to the checked exception. Here is some sample Java code to demonstrate: > try { > FileReader reader = new FileReader("file.txt"); > // Use 'reader' here. > } catch (FileNotFoundException e) { > // ... > } How do you propose to change class FileReader such that it will not throw an exception if the file does not exist? > To circumvent this, a lot of exception languages are written in a more defensive style. I don't understand this comment. Can you provide some examples?
- throwaway2037 1mo ago> brittle in the case of C# Can you explain more? In my experience, exceptions in Java, C#, and Python are all very stable and not-at-all "brittle".