Inherit from soapException

F

Flare

Hi

Im trying to create a custom Exception Hierachy for an Asp.net application.

Some of my exceptions should iherit from soapException like

MyCustomWebServiceException : soapException
{
blabla
}

Anyone have som guidelines for this? The normal rule of emplementing the
three default constructors of course dosnt count, since he they doesnt
excitst.

Google gives my nothing in this subject.

Hope someone can cheer me up :)

Reagards
Anders, DK
 
K

Kevin Spencer

Example:
MyCustomWebServiceException : soapException
{
private string _MyCustomProperty;
public string MyCustomProperty
{
get
{
return _MyCustomProperty;
}
}
public MyCustomWebServiceException(string message,
XmlQualifiedName code, string CustomParameter)
: base(string message, XmlQualifiedName code)
{
_MyCustomProperty = CustomParameter;
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
F

Flare

Thx :)

But. When i trow MyCustomWebServiceException i have problems. I cant catch
my exception as MyCustomWebServiceException, only soapException. And thats a
big problem, since My error message at the client look somthing like this.

-----> IN DANSIH SORRY BUT NOT IMPORTANT.
Elsam.Turabs.ClassLibraries.TurabsException.TurabsWebServiceException:
Calculation error---> System.DivideByZeroException: Calculation error. at
WebServiceExp.Service1.HelloWorld() in
c:\inetpub\wwwroot\webserviceexp\service1.asmx.cs:line 58 --- Slut på
staksporing af indre undtagelser --- at WebServiceExp.Service1.HelloWorld()
in c:\inetpub\wwwroot\webserviceexp\service1.asmx.cs:line 66
------>

My Message "Calculation error" is in the message besides lots og junk.

So final qusiton: Why can't i catch my custom SoapException?

The WS:
---------------
[WebMethod]
public void HelloWorld()
{
try
{
int y = 0;
int t = 1 / y ;
}
catch(System.DivideByZeroException DivideExp)
{
TurabsWebServiceException TurDBExp = new
TurabsWebServiceException("Calculation
error",System.Web.Services.Protocols.SoapException.ServerFaultCode,DivideExp
);

throw(TurDBExp); //Rethrow to client
}

The client:
----------
Exp.Service1 a = new WSExp.Service1();

try
{
a.HelloWorld();
}
catch(System.Web.Services.Protocols.SoapException et)
{
Label1.Text = et.Message;
}
 
K

Kevin Spencer

You can only catch the Exception that was thrown. If you create a Custom
Exception class, your app will have to throw it. For example, your app might
catch the SoapException, use that to create an instance of your Custom
Exception class, and then throw that. I'm not sure what exactly you're
trying to do with all this, so that' about all I can tell you.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
F

Flare

You can only catch the Exception that was thrown. If you create a Custom
Exception class, your app will have to throw it. For example, your app might
catch the SoapException, use that to create an instance of your Custom
Exception class, and then throw that. I'm not sure what exactly you're
trying to do with all this, so that' about all I can tell you.

Its hard to explain so ill try to exaplin it otherwise :)

I have an CustomSoapException wich inherit from SoapException.

To the CustomSoapException i Eg. add a property MyCustomProperty.

Now I have extended my new SoapException compared to the baseexception.

Now i catch _some_ exeption in my webservice, eg a dberror. I create an
instanse of CustomSoapException and set the MyCustomProperty to something
and rethrow this CustomSoapException to the client....right?

But here is my CORE problem. How (can i?) do i access the MyCustomProperty
on my client. Because CustomSoapException is downcastet to an ordinary
SoapException.....and MyCustomProperty is gone.

AND becauase CustomSoapException is no longer an CustomSoapException on the
client i can't catch it as CustomSoapException .

Was that more clear?
Hope so anyway.
Anders
 
S

SF

Just a guess: The CustomSoapException is not "downcasted", it is wrapped in
a SoapException.
If so, catch the SoapException on the client and rethrow its innerexception
(or just cast the inner
exception to CustomSoapException and access your custom property)
 
F

Flare

Just a guess: The CustomSoapException is not "downcasted", it is wrapped
in
a SoapException.
If so, catch the SoapException on the client and rethrow its innerexception
(or just cast the inner
exception to CustomSoapException and access your custom property)

That was my guess too. But the InnerException is null in the
SoapException......

And is try to :

catch(soapException ex)
{
string h = ((MyCustomException)ex).Message;
}

It tells me thats its an illigal cast........ ;(

Anders
 
K

Kevin Spencer

But here is my CORE problem. How (can i?) do i access the MyCustomProperty
on my client. Because CustomSoapException is downcastet to an ordinary
SoapException.....and MyCustomProperty is gone.

You lost me there. I don't understand how your Custom Exception can be cast
without casting it. It is what it is, unless someone casts it otherwise.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
F

Flare

You lost me there. I don't understand how your Custom Exception can be
cast
without casting it. It is what it is, unless someone casts it otherwise.

Hehe. Before you totally give up on me. Look at your first answere.

How do I throw your MyCustomWebServiceException and more important, how do i
catch it on the client?

My problem is taht i can't catch MyCustomWebServiceException. My guess is,
that even though i trow my exception as MyCustomWebServiceException its
"transformed" into a SoapException and cant be caught by the client as
MyCustomWebServiceException. But thats a problem because i cant access my
MyCustomProperty anymore.

If its not clear I guess I have to give up :(

Anyway this code dosent work. ...why? (I dosent catch the exception)

The Webservice (simple)
--------------------
[WebMethod]
public void hello()
{
MyCustomWebServiceException ce = new
MyCustomWebServiceException(...fill en the parameters...);

throw(ce); // We throw this exception to the client.
}

The Client (simple)
-------------------
public void ClientTester()
{
Service1 ws = new Service1(); // Create a webserice intance via the
proxy

try
{
ws.hello();
}
catch(MyCustomWebServiceException ex)
{
Label1.text = ex.Message;
}
}


}
 
K

Kevin Spencer

My apologies. I forgot that this is a Web Service. I think the following
article from the .Net SDK will be helpful with your problem:

http://msdn.microsoft.com/library/d...handlingraisingexceptionsinxmlwebservices.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.

Flare said:
You lost me there. I don't understand how your Custom Exception can be cast
without casting it. It is what it is, unless someone casts it otherwise.

Hehe. Before you totally give up on me. Look at your first answere.

How do I throw your MyCustomWebServiceException and more important, how do i
catch it on the client?

My problem is taht i can't catch MyCustomWebServiceException. My guess is,
that even though i trow my exception as MyCustomWebServiceException its
"transformed" into a SoapException and cant be caught by the client as
MyCustomWebServiceException. But thats a problem because i cant access my
MyCustomProperty anymore.

If its not clear I guess I have to give up :(

Anyway this code dosent work. ...why? (I dosent catch the exception)

The Webservice (simple)
--------------------
[WebMethod]
public void hello()
{
MyCustomWebServiceException ce = new
MyCustomWebServiceException(...fill en the parameters...);

throw(ce); // We throw this exception to the client.
}

The Client (simple)
-------------------
public void ClientTester()
{
Service1 ws = new Service1(); // Create a webserice intance via the
proxy

try
{
ws.hello();
}
catch(MyCustomWebServiceException ex)
{
Label1.text = ex.Message;
}
}


}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top