Article:
  What I Hate About Your Programming Language
Subject:   Java + Exceptions
Date:   2003-05-13 17:44:49
From:   anonymous2
Please illuminate me on when you would like to ignore an exception.
Full Threads Newest First

Showing messages 1 through 10 of 10.

  • Java + Exceptions
    2003-05-14 01:51:55  anonymous2 [View]

    You can ignore them. Just say

    void blabla(blabla) throws AnExceptionIWantToIgnore {
    moreblabla
    }
    and you can ignore the exception inside "moreblabla" ...
    • Java + Exceptions
      2003-05-14 08:52:08  anonymous2 [View]

      That isn't what the author (or others) are advocating.

      void foo()
      {
      try
      {
      }
      catch(IOException ex)
      {
      throw new RuntimeException(ex);
      }
      }

      Now you can totaly ignore it.

      This is a terrible way to program.
      • Java + Exceptions
        2003-05-14 09:44:13  anonymous2 [View]

        Actually what the author and others are defending is that you can do this:

        voif foo() {
        // ...
        }

        and if an exception is raised, it goes up to where is caught, or thrown to the toplevel.

        C++ expections are this way, you don't have to declare which exceptions can foo() trow, and it's actually Bad to do so, except when you want to guarantee the compiler it throws none (example: destructors:

        MyClass::~MyClass() throws ()
        {
        // destructors *cannot* throw or Really Evil Things will happen
        }

        any other exception spec is discouraged, they aren't optimization hints, nor actually document the exceptions that can be thrown by a function: libraries can throw their own exceptions, without limitations...)

        best regards,
        • Java + Exceptions
          2003-05-14 10:51:38  chromatic | O'Reilly AuthorO'Reilly Blogger [View]

          Yep, that's what I mean. I don't consider having to say "by the way, this method doesn't want to catch an exception, so it's going to throw it up the call stack" to be "ignoring" an exception.

          In general, I'm not a fan of writing code to tell the compiler to do nothing. Sometimes I want to ignore FileNotFound -- it's okay, I know what I'm doing.
          • Java + Exceptions
            2003-05-15 02:03:11  anonymous2 [View]

            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.
            • 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]

                  No.

                  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.