Checked Exceptions!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    #16
    Re: Checked Exceptions!


    "Jon Skeet" <skeet@pobox.co m> wrote in message
    news:MPG.199983 89ee23e0c398a27 3@news.microsof t.com...[color=blue]
    > Frans Bouma <perseus.news@x s4all.nl> wrote:[color=green][color=darkred]
    > > > Basically an implementation/override can declare that it throws fewer
    > > > exceptions, but not more.[/color]
    > >
    > > Ok, but doesn't an interface implicate a fixed definition? So if an
    > > implementator decides not to throw a given exception, does that
    > > implementator in fact not implement the complete interface? It looks[/color][/color]
    like[color=blue][color=green]
    > > Sun cut a corner on this :)[/color]
    >
    > No, not at all. The interface says what you should do if a certain type
    > of thing does wrong. If that kind of thing can't possibly go wrong in
    > your implementation, then there's no need to redeclare that.
    >
    > It's basically the Liskov Substitutabilit y Principle again - the
    > interface says, "If you've got something that implements this
    > interface, you can call this method and expect a certain result if a
    > certain thing goes wrong" - and the implementation where that certain
    > thing can't go wrong is still abiding by that interface.
    >
    > --[/color]

    This approach should work but it begs the question of whether an exception
    specification in an interface is a useful feature to have. I am not
    convinced that adding it to an interface definition yields the expected
    benefits because a real-world implementation may have many more exception
    types to deal with then the designer of the interface ever expected.

    I see the value in some flavor of checked exceptions as a design-time aid
    rather then a runtime requirement.

    There is a benefit for tools to be capable to tracing the exception graph of
    an object. At design time when using a given type I am concerned about what
    it might do so that I can write code to correctly handle the exception. I
    want the exception information to be available to me (through intellisense)
    for the same reason I want to know the arguments to a method. I think this
    would work well when dealing with concrete types; it works less well with
    late-bound types where the actual type or instance is not known until
    runtime, as typically occurs when using reflection against a type or an
    interface.


    I'd rather not have any information then have incorrect information and
    relying on interface specifications creates the problem of the documentation
    not matching the code. I think the feature is most useful when the language
    compiler can automatically generate the information at the time the type or
    interface is compiled into IL. I believe it less useful to rely on interface
    declarations as this can get out-of-date and be mismatched very easily - the
    number of ways to get this wrong is larger then the number of ways to get it
    right. It's easy to do on small projects, but on large projects (where the
    feature adds the most benefits) it's harder to maintain.

    Computers are good at automating repetitive tasks; detecting exceptions that
    can be thrown from a given implementation falls into this category.

    Note to designers of future languages - the existence of this debate is a
    clue that this aspect of programming needs a lot of thought. I have yet to
    see a language and runtime implementation that solves the general error
    handling problem correctly and consistently. We've come a long way from the
    "System error 42" days but there's a lot more work that needs to be done.

    Dave


    Comment

    • Jon Skeet

      #17
      Re: Checked Exceptions!

      Dave <kdlevine@wi.rr .com> wrote:[color=blue]
      > This approach should work but it begs the question of whether an exception
      > specification in an interface is a useful feature to have. I am not
      > convinced that adding it to an interface definition yields the expected
      > benefits because a real-world implementation may have many more exception
      > types to deal with then the designer of the interface ever expected.[/color]

      Most of the time, it works very well, in my experience. The interface
      designer needs to be careful, of course, but they should include
      exceptions which match what was meant to happen, and allow exception
      chaining to give specific exceptions. For instance, when performing
      some database operation, it makes sense for a SqlException to be thrown
      even if underlying that there was an IOException (which is then chained
      onto the SqlException).

      Where it becomes a little messier is where there will *always* be a
      chained exception, and the original exception is virtually never
      meaningful in itself - for instance, if an iterator had an
      IterationExcept ion, it would usually be because something underneath
      had caused a problem.

      Having used both Java and C# fairly extensively now, I can see how not
      having checked exceptions makes the C# coding quicker - particularly
      when you're writing quick test code rather than production code - but I
      still think the Java approach prods the developer to consider possibly
      error routes more.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Frans Bouma

        #18
        Re: Checked Exceptions!

        Jon Skeet <skeet@pobox.co m> wrote in
        news:MPG.199983 89ee23e0c398a27 3@news.microsof t.com:
        [color=blue]
        > Frans Bouma <perseus.news@x s4all.nl> wrote:[color=green][color=darkred]
        >> > Basically an implementation/override can declare that it throws fewer
        >> > exceptions, but not more.[/color]
        >>
        >> Ok, but doesn't an interface implicate a fixed definition? So
        >> if an
        >> implementator decides not to throw a given exception, does that
        >> implementator in fact not implement the complete interface? It looks
        >> like Sun cut a corner on this :)[/color]
        >
        > No, not at all. The interface says what you should do if a certain type
        > of thing does wrong. If that kind of thing can't possibly go wrong in
        > your implementation, then there's no need to redeclare that.[/color]

        that's for the caller/user of the interface. What about the
        developer of the code which implements the interface? That developer now
        has to implement all described functionality (logically), however what
        would/should/does the compiler do when that developer doesn't throw an
        exception defined in such interface? The compiler barfs when a method is
        not implemented, however does it also cough up an error if a given
        exception is not implemented?

        Also, if the developer of the implementation DOESN'T throw any of
        the exceptions specified but another one, how would the code using the
        interface gain anything? The interface clearly doesn't describe the
        reality. (and thus makes the whole purpose, although nice to have,
        useless)

        FB


        --
        Solutions Design : http://www.sd.nl
        My open source .NET Software : http://www.sd.nl/software
        My .NET Blog : http://weblogs.asp.net/FBouma
        -------------------------------------------------------------------------

        Comment

        • Jon Skeet

          #19
          Re: Checked Exceptions!

          Frans Bouma <perseus.news@x s4all.nl> wrote:[color=blue][color=green]
          > > No, not at all. The interface says what you should do if a certain type
          > > of thing does wrong. If that kind of thing can't possibly go wrong in
          > > your implementation, then there's no need to redeclare that.[/color]
          >
          > that's for the caller/user of the interface. What about the
          > developer of the code which implements the interface? That developer now
          > has to implement all described functionality (logically), however what
          > would/should/does the compiler do when that developer doesn't throw an
          > exception defined in such interface? The compiler barfs when a method is
          > not implemented, however does it also cough up an error if a given
          > exception is not implemented?[/color]

          No - because the only reason for not throwing the exception is if the
          condition under which the interface says the exception should be thrown
          doesn't/can't happen.
          [color=blue]
          > Also, if the developer of the implementation DOESN'T throw any of
          > the exceptions specified but another one, how would the code using the
          > interface gain anything? The interface clearly doesn't describe the
          > reality. (and thus makes the whole purpose, although nice to have,
          > useless)[/color]

          You're putting up a straw man here of the developer being pretty much
          deliberately irresponsible. The interface should describe sufficiently
          general exceptions so that anything that can go wrong can be mapped to
          one of the exceptions, often including chaining (so that even if the
          cause isn't immediately as described, it is the same type of thing).
          The developer then obeys the interface by throwing the appropriate
          exception, possibly chaining a more fundamental one.

          For instance, in a database implemented directly over a file system, if
          an IO error occurred when reading a file, you'd still throw a database-
          type exception (as that's what the caller is dealing with), chained to
          an IOException (as that's what the nitty-gritty of what went wrong is
          about).

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • ncaHammer

            #20
            Re: Checked Exceptions!

            "Frans Bouma" <perseus.news@x s4all.nl> wrote in message
            news:Xns93CE678 A35CCCperseusne wsxs4allnl@207. 46.248.16...[color=blue]
            > Checked exceptions are in theory a nice idea,[/color]

            I find checked exceptions awful in theory too !
            Checked exceptions abuse the exception mechanism by using it (basically)
            instead of return values

            They tie the *caller* to a contract, destroying the basic purpose of why we
            have structured exceptions in the first place.

            While exceptions can trace the stack for a handler without limitations (many
            levels), checked exceptions enforce a first level resolution always.

            I wonder who invented them ?



            Comment

            • Jon Skeet

              #21
              Re: Checked Exceptions!

              ncaHammer <ncahammer@nos. pamhot.mail.com > wrote:[color=blue]
              > "Frans Bouma" <perseus.news@x s4all.nl> wrote in message
              > news:Xns93CE678 A35CCCperseusne wsxs4allnl@207. 46.248.16...[color=green]
              > > Checked exceptions are in theory a nice idea,[/color]
              >
              > I find checked exceptions awful in theory too !
              > Checked exceptions abuse the exception mechanism by using it (basically)
              > instead of return values
              >
              > They tie the *caller* to a contract, destroying the basic purpose of why we
              > have structured exceptions in the first place.
              >
              > While exceptions can trace the stack for a handler without limitations (many
              > levels), checked exceptions enforce a first level resolution always.[/color]

              Only if you think you always have to actually catch them, or if you
              deem letting them automatically propagate up the stack to be "first
              level resolution".

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • ncaHammer

                #22
                Re: Checked Exceptions!

                "Jon Skeet" <skeet@pobox.co m> wrote in message
                news:MPG.1999ea 2a3e90678d98a27 d@news.microsof t.com...[color=blue]
                > Only if you think you always have to actually catch them, or if you
                > deem letting them automatically propagate up the stack to be "first
                > level resolution".[/color]

                altering method signature so the exception can propagate *is*
                "first level resolution" too




                Comment

                • Jon Skeet

                  #23
                  Re: Checked Exceptions!

                  ncaHammer <ncahammer@nos. pamhot.mail.com > wrote:[color=blue][color=green]
                  > > Only if you think you always have to actually catch them, or if you
                  > > deem letting them automatically propagate up the stack to be "first
                  > > level resolution".[/color]
                  >
                  > altering method signature so the exception can propagate *is*
                  > "first level resolution" too[/color]

                  I don't think that deciding not to deal with it counts as resolution,
                  myself.

                  I don't think we're going to agree on this issue either though...

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • OvErboRed

                    #24
                    Re: Checked Exceptions!

                    Frans Bouma <perseus.news@x s4all.nl> wrote in
                    news:Xns93CE678 A35CCCperseusne wsxs4allnl@207. 46.248.16:
                    [color=blue]
                    > So are the pro-checked exception reasons!
                    > If documentation about which exceptions a method can throw is not
                    > enough, what is?
                    > different implementations which can throw different exceptions,
                    > however when you define them as checked in the interface, you are not
                    > able to do that.[/color]

                    Far from being an issue of sloppiness, the absence of checked exceptions
                    presents a very real limitation to the design of well-encapsulated
                    components. Documentation is important, of course; nobody's questioning
                    that. However, if documentation were enough, then return- and parameter-
                    checking would be extraneous as well. You will probably argue that
                    exceptions are different because they are more closely related to the
                    implementation. However, that's the crux of the problem: interfaces are
                    supposed to prevent implementation details from being exposed.

                    But just as you're not limited to returning the exact type specified in
                    a method signature, you aren't being crippled by checked exceptions.
                    Make no mistake about it. As long as any inner exception is a subclass
                    of or wrapped in an instance of the throwable types that are permitted
                    by the interface, nothing prevents you from passing back any extended
                    data specific to the implementation at hand.

                    In fact, checked exceptions offer a safer way to throw your own
                    exceptions, because you'll know that doing so won't break the calling
                    code if it's not expecting your particular exception's runtime type.
                    That's what it all boils down to: by replacing the underlying
                    implementation without making any guarantees on the type of data to
                    expect, you aren't running the risk of an unexpected exception bubbling
                    up clear off the stack or of needing to rewrite code everywhere, as is
                    currently the case with .NET.



                    Finally, if you're still not convinced, remember that you'd be free to
                    automagically add "throws Exception" to all your methods and resume your
                    work hassle-free. =)

                    Comment

                    • Frans Bouma

                      #25
                      Re: Checked Exceptions!

                      OvErboRed <overboredNO@SP AMoverbored.net > wrote in
                      news:Xns93CEA83 BC706Byangstaov erbored@207.46. 248.16:
                      [color=blue]
                      > Frans Bouma <perseus.news@x s4all.nl> wrote in
                      > news:Xns93CE678 A35CCCperseusne wsxs4allnl@207. 46.248.16:
                      >[color=green]
                      >> So are the pro-checked exception reasons!
                      >> If documentation about which exceptions a method can throw is not
                      >> enough, what is?
                      >> different implementations which can throw different exceptions,
                      >> however when you define them as checked in the interface, you are not
                      >> able to do that.[/color]
                      >
                      > Far from being an issue of sloppiness, the absence of checked exceptions
                      > presents a very real limitation to the design of well-encapsulated
                      > components. Documentation is important, of course; nobody's questioning
                      > that. However, if documentation were enough, then return- and parameter-
                      > checking would be extraneous as well. You will probably argue that
                      > exceptions are different because they are more closely related to the
                      > implementation. However, that's the crux of the problem: interfaces are
                      > supposed to prevent implementation details from being exposed.[/color]

                      exactly, so interfaces should not have any information about the
                      implementation details. No offence, but if an interface defines 'Throws
                      FileIOException ' it clearly defines that the implementation can/should
                      access files, however that's probably not the case. So it tells the caller
                      FALSE information. This is something different than an implementation of
                      the interface defines that it throws FileIOException , since it then is
                      legitimate to say 'I throw exception xyz', because we're talking about a
                      given implementation then.
                      [color=blue]
                      > But just as you're not limited to returning the exact type specified in
                      > a method signature, you aren't being crippled by checked exceptions.
                      > Make no mistake about it. As long as any inner exception is a subclass
                      > of or wrapped in an instance of the throwable types that are permitted
                      > by the interface, nothing prevents you from passing back any extended
                      > data specific to the implementation at hand.[/color]

                      True, but for exceptions I'd like to make an erm.. exception :) If
                      the returntype is a given baseclass, code calling the method can work with
                      the base class, otherwise they wouldn't have called the method. However,
                      an exception thrown should TELL what is wrong, not being a flag that says
                      'Hi, something was wrong, look inside my tree of inner exceptions to see
                      what's wrong', because you can't catch inner exceptions. Exceptions thrown
                      therefore have to match exactly what went wrong. Encapsulation of
                      exceptions in higher order exception classes is therefore making
                      exceptions a moot technology.
                      [color=blue]
                      > In fact, checked exceptions offer a safer way to throw your own
                      > exceptions, because you'll know that doing so won't break the calling
                      > code if it's not expecting your particular exception's runtime type.
                      > That's what it all boils down to: by replacing the underlying
                      > implementation without making any guarantees on the type of data to
                      > expect, you aren't running the risk of an unexpected exception bubbling
                      > up clear off the stack or of needing to rewrite code everywhere, as is
                      > currently the case with .NET.[/color]

                      Of course you do run the risk! If a new implementation implements
                      the interface differently and has to encapsulate a new exception into a
                      higher order exception to avoid breakage of the exception definition in
                      the interface, you're creating poopcode.

                      I see exceptions as: if the DIRECT caller can recover from a given
                      set of exceptions, catch these and recover there, and bubble up the rest,
                      because these are fatal for that position in the call stack and perhaps a
                      higher level in the callstack can recover from these. New exceptions in a
                      new implementation of an interface can be handled then as such, they end
                      up as being 'fatal' and will report an error message or abort a
                      transaction or whatever. Eric Gunnerson has enlisted these situations
                      before, in the mile long thread about the same topic :)
                      [color=blue]
                      > Finally, if you're still not convinced, remember that you'd be free to
                      > automagically add "throws Exception" to all your methods and resume your
                      > work hassle-free. =)[/color]

                      Yeah right. If that is necessary 'checked exceptions' is useless.
                      If a given rule enforces you to do something but can be overcome by
                      cheating (like throws Exception) the 'enforcement' is not mandatory
                      anymore so callers of code defining exceptions can't rely on the list they
                      see and therefore you can also just say 'no' to checked exceptions and
                      save you all the trouble.

                      FB

                      --
                      Solutions Design : http://www.sd.nl
                      My open source .NET Software : http://www.sd.nl/software
                      My .NET Blog : http://weblogs.asp.net/FBouma
                      -------------------------------------------------------------------------

                      Comment

                      • Dave

                        #26
                        Re: Checked Exceptions!


                        "Jon Skeet" <skeet@pobox.co m> wrote in message
                        news:MPG.199994 57a840fd2998a27 5@news.microsof t.com...[color=blue]
                        >
                        > Most of the time, it works very well, in my experience. The interface
                        > designer needs to be careful, of course, but they should include
                        > exceptions which match what was meant to happen, and allow exception
                        > chaining to give specific exceptions. For instance, when performing
                        > some database operation, it makes sense for a SqlException to be thrown
                        > even if underlying that there was an IOException (which is then chained
                        > onto the SqlException).
                        >[/color]

                        I disagree. Knowing that the ultimate case was, e.g. FileIOException tells
                        the user that the corrective action to take involves the file system rather
                        then something else. Mapping this to a SQLException is misleading.
                        [color=blue]
                        > Where it becomes a little messier is where there will *always* be a
                        > chained exception, and the original exception is virtually never
                        > meaningful in itself - for instance, if an iterator had an
                        > IterationExcept ion, it would usually be because something underneath
                        > had caused a problem.
                        >[/color]

                        If calling code has to examine the inner exception in order to determine
                        what happened and how to take corrective action then much of the benefit of
                        checked exceptions is lost. Hiding details of the cause of an error is, in
                        many cases, the wrong approach to take.

                        My current position on exceptions is to use exception chaining extensively.
                        At each major logical transition I add another try-catch layer; if an
                        exception is caught it adds some context information and rethrows the
                        exception. At some higher layer (e.g. the UI) it unrolls all the exceptions
                        and presents them to the user. Corrective action is taken at the lowest
                        level possible, which varies based on the operation being performed.
                        [color=blue]
                        > Having used both Java and C# fairly extensively now, I can see how not
                        > having checked exceptions makes the C# coding quicker - particularly
                        > when you're writing quick test code rather than production code - but I
                        > still think the Java approach prods the developer to consider possibly
                        > error routes more.
                        >
                        > --[/color]
                        I am in favor of there being more discipline in error/exception handling;
                        this is an area that needs a lot more work, both conceptually and in how
                        people use it.

                        In this discussion there are a lot of unexamined assumptions. Many of the
                        arguments make sense when applied to the small case but tend to make less
                        sense when applied to the situations most developers run into and in large
                        systems.

                        For example, should a web service throw exceptions across a web method
                        boundary (I know it can, but should it)? Should the web method define what
                        exceptions it throws (if the answer is yes then a lot of work needs to be
                        done to tools such as wsdl).

                        It seems to me that checked exception definitions are one of those ideas
                        that sounds great on paper but in practice either create new problems or
                        don't fully solve the current problem. I'd like to see more theoretical work
                        done in this area. I also believe that treating exceptions thrown as part of
                        the type definition and including it in the metadata would make it more
                        practical to implement an exception management strategy.




                        Comment

                        • Jon Skeet

                          #27
                          Re: Checked Exceptions!

                          Dave <kdlevine@wi.rr .com> wrote:[color=blue][color=green]
                          > > Most of the time, it works very well, in my experience. The interface
                          > > designer needs to be careful, of course, but they should include
                          > > exceptions which match what was meant to happen, and allow exception
                          > > chaining to give specific exceptions. For instance, when performing
                          > > some database operation, it makes sense for a SqlException to be thrown
                          > > even if underlying that there was an IOException (which is then chained
                          > > onto the SqlException).[/color]
                          >
                          > I disagree. Knowing that the ultimate case was, e.g. FileIOException tells
                          > the user that the corrective action to take involves the file system rather
                          > then something else. Mapping this to a SQLException is misleading.[/color]

                          I guess we disagree then. In my view, the exception thrown should
                          always have something to do with the activity the caller is dealing
                          with.
                          [color=blue][color=green]
                          > > Where it becomes a little messier is where there will *always* be a
                          > > chained exception, and the original exception is virtually never
                          > > meaningful in itself - for instance, if an iterator had an
                          > > IterationExcept ion, it would usually be because something underneath
                          > > had caused a problem.[/color]
                          >
                          > If calling code has to examine the inner exception in order to determine
                          > what happened and how to take corrective action then much of the benefit of
                          > checked exceptions is lost. Hiding details of the cause of an error is, in
                          > many cases, the wrong approach to take.[/color]

                          It's not hiding them - it's storing them but giving a more general view
                          at the top level.

                          In my experience, "corrective action" is rarely taken due to
                          exceptions. I rarely try to fix something and then try again - I
                          usually log/report the error, and deal with the fact that I couldn't do
                          what I wanted to (whatever that entails). There are occasions where
                          it's possible to try again with something different, but they're not
                          common IME.
                          [color=blue]
                          > My current position on exceptions is to use exception chaining extensively.
                          > At each major logical transition I add another try-catch layer; if an
                          > exception is caught it adds some context information and rethrows the
                          > exception. At some higher layer (e.g. the UI) it unrolls all the exceptions
                          > and presents them to the user. Corrective action is taken at the lowest
                          > level possible, which varies based on the operation being performed.[/color]

                          That sounds similar to what I was proposing though...
                          [color=blue][color=green]
                          > > Having used both Java and C# fairly extensively now, I can see how not
                          > > having checked exceptions makes the C# coding quicker - particularly
                          > > when you're writing quick test code rather than production code - but I
                          > > still think the Java approach prods the developer to consider possibly
                          > > error routes more.
                          > >
                          > > --[/color]
                          > I am in favor of there being more discipline in error/exception handling;
                          > this is an area that needs a lot more work, both conceptually and in how
                          > people use it.[/color]

                          Agreed. I'm not saying I've got all the answers or that I do things
                          particularly well, either :(
                          [color=blue]
                          > In this discussion there are a lot of unexamined assumptions. Many of the
                          > arguments make sense when applied to the small case but tend to make less
                          > sense when applied to the situations most developers run into and in large
                          > systems.
                          >
                          > For example, should a web service throw exceptions across a web method
                          > boundary (I know it can, but should it)? Should the web method define what
                          > exceptions it throws (if the answer is yes then a lot of work needs to be
                          > done to tools such as wsdl).[/color]

                          I'd say yes to both, offhand - but I haven't used web services much, to
                          be honest.
                          [color=blue]
                          > It seems to me that checked exception definitions are one of those ideas
                          > that sounds great on paper but in practice either create new problems or
                          > don't fully solve the current problem. I'd like to see more theoretical work
                          > done in this area. I also believe that treating exceptions thrown as part of
                          > the type definition and including it in the metadata would make it more
                          > practical to implement an exception management strategy.[/color]

                          I certainly don't think they fully solve the current problem - but in
                          many cases I believe they solve the current problem better than not
                          having anything but documentation.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          If replying to the group, please do not mail me too

                          Comment

                          Working...