What I Hate About Your Programming Language
Subject:   Java + Exceptions
Date:   2003-05-15 02:03:11
From:   anonymous2
Response to: Java + Exceptions

No you clearly do not know what you are doing. The whole point of checked exceptions is that the compiler can help verify that you are not forgetting to handle them.

You have to catch the FileNotFoundException somewhere... might as well make the code clear as to what is going on.

If you are happy to have your program exit with a stack trace... then you really really do not have a clue as to what you are doing.

Full Threads Newest First

Showing messages 1 through 5 of 5.

  • Java + Exceptions
    2003-05-15 10:11:33  chromatic | O'Reilly AuthorO'Reilly Blogger [View]

    Yes, that's exactly my gripe.

    If I don't think a missing file is an exceptional condition within the current program, I don't really have the option to ignore FileNotFoundException. I either catch it explicitly or let it propogate upwards and crash with a stack trace.

    I'd like to be able to ignore (as in, "not have to write any code to tell the language not to do something") certain exceptions sometimes. I don't have that option in Java, unless I do a bit of metaprogramming or rethrow exceptions as run-time exceptions, or some other technique.

    The problem with a mandatory, checked exception system is that what Sun thinks are exceptional conditions are not always what I think are exceptional conditions in certain situations.
    • Java + Exceptions
      2003-05-15 15:22:48  anonymous2 [View]

      You are missing the point - a user should never see a stack trace. If you think missing the file is a good time to bail on the program than catch it and do a System.exit.

      If you are selectivly able to ignore exceptions you wind up either missing important cases or writing catchal (like ... or Exception) and handling exceptions it a totaly wrong manener.

      You should not be using exceptions to automagically end the program - that is the height of lazyness.
      • Java + Exceptions
        2003-05-15 15:40:59  chromatic | O'Reilly AuthorO'Reilly Blogger [View]


        All I'm saying is that sometimes I don't consider certain things exceptional. Java won't let me ignore them. I either have to deal with the exception or let it crash.

        I don't like either option. If I know the file's not likely to be there and my program isn't going to crash if it's not there, it's not an exceptional condition. Why do I have to do busywork to make my intent clear?
        • Java + Exceptions
          2003-05-15 16:42:08  anonymous2 [View]

          Why would you open a file for reading if you don't think it is going to be there?

          If you ask to open a file and it does not exist then, yes, it is an exceptional condition.

          Also if you wrote the code in, say, C and you didn't check the return of fopen you would "need" to put a comment to make the intent clear. Why put a comment that can easily be wrong after modifying the code if the compiler can do it for free?

          Also if you open a file AND it doesn't exist you would need to check that the return value was not null... messy boilerplate code that is easy to get wrong and hides the logic of the program.

          Please give a clear, concise, somewhat complete example of what you are saying you want - to open a file that may/may not exist and have it be fine either way AND detail what you would rather go through than deal with exceptions.
          • Java + Exceptions
            2003-05-15 22:25:54  anonymous2 [View]

            What the other guy wants is supported in other languages. I won't tell you which as you might criticize the language just because of that:). There are cases that you may just want to ignore an exception. The language I know well just has you 'flick a switch' before executing the code and that code will run just as it would have executed OK even though the results may vary.

            Basically the switch stops any (or most) errors from percolating from the system into your function. Some systems are very explicit with their messages so I may have to catch say 20 exception messages to have the function 'continu'.

            Another ''envvar'' may later be tested if something went wrong indeed if you want to - and did not reuse that ''envvar''.

            So if I open a file - which can go wrong for maybe a hundred reasons with a dozen error codes on this system - a can flick the switch and later test whether it was successful or not - which is the only thing I am interested in. (In this case I am still handling the exception, I know.)

            This is obviously not Java, but it makes me understand the question.