Checked Exception

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

    #1

    Checked Exception

    Hi,

    Unlike Java, there is no Checked Exception in C#.
    Is there any work around for this.

    For example, I am declaring an interface and inside this interface I have a
    function and I would like to force this function to throw an exception in
    the implementation.

    public interface IMyInterface
    {
    void foo () throws MyException;
    }

    how can I do that?

    Thanks,
    Lalit


  • Daniel O'Connell

    #2
    Re: Checked Exception


    You can't, atleast not currently.
    The best you can do is provide explicit documentation

    "Lalit Parashar" <lalitp@mapleus a.com.NOSPAM> wrote in message
    news:ei9qZZifDH A.164@tk2msftng p13.phx.gbl...[color=blue]
    > Hi,
    >
    > Unlike Java, there is no Checked Exception in C#.
    > Is there any work around for this.
    >
    > For example, I am declaring an interface and inside this interface I have[/color]
    a[color=blue]
    > function and I would like to force this function to throw an exception in
    > the implementation.
    >
    > public interface IMyInterface
    > {
    > void foo () throws MyException;
    > }
    >
    > how can I do that?
    >
    > Thanks,
    > Lalit
    >
    >[/color]


    Comment

    • Jeff Louie

      #3
      Re: Checked Exception

      Yes. In the NAME. IMyInterfaceWit hExceptions <g>

      Regards,
      Jeff[color=blue]
      >The best you can do is provide explicit documentation<[/color]



      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Dominic Paquette

        #4
        Re: Checked Exception

        Hi, in the example you gave in java, someone implementing the foo function
        does not need to throw any exception you could do this:

        public class MyClass implements IMyInterface
        {
        void foo ()
        {
        }
        }

        and it would compile fine... I haven't tried it but I'm pretty sure
        Dominic

        "Lalit Parashar" <lalitp@mapleus a.com.NOSPAM> a écrit dans le message de
        news: ei9qZZifDHA.164 @tk2msftngp13.p hx.gbl...[color=blue]
        > Hi,
        >
        > Unlike Java, there is no Checked Exception in C#.
        > Is there any work around for this.
        >
        > For example, I am declaring an interface and inside this interface I have[/color]
        a[color=blue]
        > function and I would like to force this function to throw an exception in
        > the implementation.
        >
        > public interface IMyInterface
        > {
        > void foo () throws MyException;
        > }
        >
        > how can I do that?
        >
        > Thanks,
        > Lalit
        >
        >[/color]


        Comment

        • Jason Smith

          #5
          Re: Checked Exception

          As far as I can tell, this was a design decision by Microsoft. As a long
          time Java programmer, I have come to think it is a good one. It is usually
          a bad idea to try to force only a certain type of exception, and in fact it
          doesn't work that way in Java anyway. RuntimeExceptio ns are not checked,
          and lots of code can throw things like null-pointer exceptions or divide by
          zero exceptions, and Java doesn't catch it. You never see these sorts of
          things documented, but they can be thrown all over the place by all sorts of
          code.

          Usually what happens when you force the users of your library to use
          specific exception types is that they have to catch all the Exceptions and
          just rethrow them as your exception type, or as a RuntimeExceptio n. Silly,
          and a waste of effort. Why would I want to wrap a divide-by-zero exception
          inside a SocketException , for instance? It just obfuscates the source of
          the error.

          The other thing that happens is you end up writing stupid exception handlers
          for exceptions that are declared but which never happen. If your code is
          written in such a way that the exception is never going to occur, you still
          have to catch the exception and deal with it because the compiler says so.
          I remember that Xerces has one of those for the DocumentFactory , or one of
          those objects. You call the function, it works every time. Never, ever
          fails. Yet it supposedly could throw an exception, so you have to deal with
          a meaningless try catch structure. Silliness.

          That one feature in Java has caused as many arguments as whether VB6 can be
          considered an OO language.

          Let your exceptions run free! It really does work quite well that way.
          Document and catch the exceptions you are interested in, and let the rest
          bubble up the stack. If they are a problem, the framework will let you
          know.

          "Lalit Parashar" <lalitp@mapleus a.com.NOSPAM> wrote in message
          news:ei9qZZifDH A.164@tk2msftng p13.phx.gbl...[color=blue]
          > Hi,
          >
          > Unlike Java, there is no Checked Exception in C#.
          > Is there any work around for this.
          >
          > For example, I am declaring an interface and inside this interface I have[/color]
          a[color=blue]
          > function and I would like to force this function to throw an exception in
          > the implementation.
          >
          > public interface IMyInterface
          > {
          > void foo () throws MyException;
          > }
          >
          > how can I do that?
          >
          > Thanks,
          > Lalit
          >
          >[/color]


          Comment

          Working...