Custom Exceptions

Discussion in 'ASP .Net Web Services' started by Chris Dunaway, Jan 13, 2004.

  1. I'm creating a Web service and a Windows Forms application to consume it.
    My question is about throwing a custom exception inside the WebService.
    Can that be done, and can the custom web service be handled in the Windows
    Forms app?

    For example, suppose in my WebService, I have the following exception
    class:

    '\\\\\
    <Serializable()> _
    Public Class WebServiceException
    Inherits ApplicationException <<<<<<< Is this correct?

    'Code for exception class here

    End Class
    ../////

    And then within my web method:

    '\\\\\
    <WebMethod()> _
    Public Sub SomeWebMethod()

    If SomeCondition Then
    Throw New WebServiceException
    End If
    End Sub
    '/////


    On the Windows Forms client, I can only trap for generic exceptions not my
    custom exception type:

    Try
    WebService.SomeWebMethod
    Catch ex As WebServiceException <<<<<<<<< This doesn't work
    'Handle it
    End Try


    Anyone know of a way to achieve this?

    Thanks for any pointers

    --
    Chris

    To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
    address.
    Chris Dunaway, Jan 13, 2004
    #1
    1. Advertising

  2. Chris Dunaway

    Jan Tielens Guest

    Check out following article that covers this topic:
    http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/soapexcept
    ions.mspx

    Throwing SoapExceptions

    Summary:

    Sometimes things can go wrong when code is executed, so Exceptions are
    thrown. When your application exposes services through a webservice layer,
    SoapExceptions are thrown from the server to the client. By default normal
    Exceptions are converted to a SoapException, resulting in a rather ugly
    Exception containing all information concatenated in a long String. But
    luckily the SOAP protocol allows any XML document to be included in SOAP
    error messages. This article shows how to build some helper classes to
    easily pass error information from the server to the client through a
    webservice layer.

    --
    Greetz

    Jan Tielens
    ________________________________
    Read my weblog: http://weblogs.asp.net/jan


    "Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
    news:pjwg5qhhnul.1w1a09j3evsz7$...
    > I'm creating a Web service and a Windows Forms application to consume it.
    > My question is about throwing a custom exception inside the WebService.
    > Can that be done, and can the custom web service be handled in the Windows
    > Forms app?
    >
    > For example, suppose in my WebService, I have the following exception
    > class:
    >
    > '\\\\\
    > <Serializable()> _
    > Public Class WebServiceException
    > Inherits ApplicationException <<<<<<< Is this correct?
    >
    > 'Code for exception class here
    >
    > End Class
    > ./////
    >
    > And then within my web method:
    >
    > '\\\\\
    > <WebMethod()> _
    > Public Sub SomeWebMethod()
    >
    > If SomeCondition Then
    > Throw New WebServiceException
    > End If
    > End Sub
    > '/////
    >
    >
    > On the Windows Forms client, I can only trap for generic exceptions not my
    > custom exception type:
    >
    > Try
    > WebService.SomeWebMethod
    > Catch ex As WebServiceException <<<<<<<<< This doesn't work
    > 'Handle it
    > End Try
    >
    >
    > Anyone know of a way to achieve this?
    >
    > Thanks for any pointers
    >
    > --
    > Chris
    >
    > To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
    > address.
    Jan Tielens, Jan 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Dunaway

    Custom Exceptions with Web Services

    Chris Dunaway, Jan 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    402
    Shiv Kumar
    Jan 13, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    29,973
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    998
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    585
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    592
Loading...

Share This Page