473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Throwing Errors in Web Service

I was wondering how to handle an error in a web service. If our web service
encounters a connection error or any other error what is the best practice
for returning an error? Do you return a blank result with an error attached
or is there some other way to do this?

thanks,
Anthony
Aug 23 '06 #1
2 1887
>I was wondering how to handle an error in a web service. If our web
>service
encounters a connection error or any other error what is the best practice
for returning an error? Do you return a blank result with an error
attached
or is there some other way to do this?

SOAPFault is just exactly for the same.

--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.org
http://www.edujini.in | http://webservices.edujini.in
-------------------
Aug 23 '06 #2
What I do is trap the exception within the Web Service and then throw
and then throw it as a SoapException.

In Web Service:
Try
' test for some condition and treat it like a system
exception
If Something_Bad_Happened() Then
Throw New System.Exception("Something Bad Happened
Exception")
End If

Catch ex As System.Exception

' certain types of exceptions you might re-throw as a
SoapExceptions
' other types you might ignore or handle on the web service
side.
If ex.Message = "Something Bad Happened Exception" Then
' Build the detail element of the SOAP fault.
Dim doc As New System.Xml.XmlDocument
Dim node As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
SoapException.DetailElementName.Name, _
SoapException.DetailElementName.Namespace)

' Build specific details for the SoapException.
' Add details as the first child to the XML element
that is already created.
Dim details As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
"mySpecialInfo1", "http://tempuri.org/")

' Add details2 as the first child to the XML element
that is already created.
Dim details2 As System.Xml.XmlNode = _
doc.CreateNode(XmlNodeType.Element, _
"mySpecialInfo2", "http://tempuri.org/")
Dim attr As XmlAttribute = doc.CreateAttribute("t", _
"attrName", "http://tempuri.org/")
attr.Value = "attrValue"
details2.Attributes.Append(attr)

' Append the two child elements to the detail node.
node.AppendChild(details)
node.AppendChild(details2)

'Throw SOAP exception to be returned by Web Service
Dim se As New SoapException("The Web Service Failed
Because Something Really Bad Happened.", _
SoapException.ServerFaultCode, _
HttpContext.Current.Request.Url.AbsoluteUri, node)
Throw se
Else
Throw ex
End If
End Try

On Client Side:

Try
WS.MyWebServiceMethodCall()
Catch Se As System.Web.Services.Protocols.SoapException
MsgBox(Se.Message,
MsgBoxStyle.Critical, "Soap Exception in
thrown in Web Service")
Catch ex As System.Exception
MsgBox(ex.Message, _
MsgBoxStyle.Critical, "Exception in
Client")
End Try

This way you can distinguish between exceptions caused by the Web
Service and exceptions thrown in the client.

The error thrown by the Web Service may have originally been an
exception or just some error condition that you decide to throw as a
SoapException.

You don't really need to worry about the Web Service parameters since
once the Soap Exception is thrown they are no longer relevant.

If the exception is originally thrown by the web service, with a little
more work, you can throw it as a SoapException with the inner exception
containing the original message string.

I'm not sure if this is the best practice, but if you hunt around on
msdn.com I think there are some other examples of how to use
SoapException. I just threw this together so it's a bit sloppy, but
you get the idea...

Good Luck,
-Paul
Anthony Biondo Jr wrote:
I was wondering how to handle an error in a web service. If our web service
encounters a connection error or any other error what is the best practice
for returning an error? Do you return a blank result with an error attached
or is there some other way to do this?

thanks,
Anthony
Aug 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
4
by: Eric Lilja | last post by:
Hello, in my program I have a function (pseudo code): void start_mysql_service() { obtain handle start mysql service using handle if start fails close handle and throw an exception...
12
by: Russ | last post by:
Hello. My new dev machine is running XP Pro. In the past all equipment has only used Windows 2000. I have had a lot of problems getting my projects up and running on the new machine. The current...
1
by: René Meinecke | last post by:
hi there, i have created a webservice which is running well on the developer computer. for this i also created a windows installer setup which also works well on the developer computer, but if i...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
4
by: Claire | last post by:
I'm writing a realtime monitoring application acting as a Windows service. This service communicates to some instrumentation via a third party dll. TCP is the transport mechanism over the network....
3
by: Joel D. Kraft | last post by:
I've been logging a lot of fatal errors on my site lately, and I have not been able to figure out why. First, my site has error handling pages defined globally as shown below. They are not...
3
by: Baheri | last post by:
Can some some guide me to a good link on how to throw errors in a webservice? Also if there is a standard way of defining different degress of erros while exposing your webservice to thrid parties?...
21
by: Chris M. Thomasson | last post by:
Is it every appropriate to throw in a dtor? I am thinking about a simple example of a wrapper around a POSIX file... ________________________________________________________________________ class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.