8 ms·
It 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 except
by throwaway2037 2mo ago
It 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?