W
Water Cooler v2
When a Web Service throws an exception to the caller/client, the
exception is always cast to SoapException regardless of the actual type
of the exception thrown. In such a scenario, how would you think it is
appropriate for a Web services client to determine the exact type of
exception?
Consider this example.
CLIENT
class Client
{
[STAThread]
static void Main(string[] args)
{
using(localhost.ExceptionThrowerService ets = new
ExceptionCatcher.localhost.ExceptionThrowerService())
{
try
{
ets.throwCustomException();
}
catch(System.Exception e)
{
/*How would you determine here whether the type of e is
MyCustomException or some other one
Given that MyCustomException is given inside the Web service.*/
Console.WriteLine(e.GetType().ToString());
}
Console.ReadLine();
}
}
}
SERVER
namespace ExceptionThrower
{
public class ExceptionThrowerService : System.Web.Services.WebService
{
[WebMethod]
public void throwCustomException()
{
Trace.WriteLine("Throwing custom exception now.");
throw new MyCustomException();
}
} //End of class ExceptionThrower
public class MyCustomException: System.Exception
{
private string mDetail = "I am the best guy.";
public MyCustomException()
{
mDetail = "I am *still* the best guy.";
}
public MyCustomException(string lDetail)
{
this.Detail += ("\n" + lDetail);
}
public string Detail
{
get
{
return mDetail;
}
set
{
mDetail = value;
Trace.WriteLine("Setting the value of mDetail...");
}
}
}//End of class MyCustomException
}
exception is always cast to SoapException regardless of the actual type
of the exception thrown. In such a scenario, how would you think it is
appropriate for a Web services client to determine the exact type of
exception?
Consider this example.
CLIENT
class Client
{
[STAThread]
static void Main(string[] args)
{
using(localhost.ExceptionThrowerService ets = new
ExceptionCatcher.localhost.ExceptionThrowerService())
{
try
{
ets.throwCustomException();
}
catch(System.Exception e)
{
/*How would you determine here whether the type of e is
MyCustomException or some other one
Given that MyCustomException is given inside the Web service.*/
Console.WriteLine(e.GetType().ToString());
}
Console.ReadLine();
}
}
}
SERVER
namespace ExceptionThrower
{
public class ExceptionThrowerService : System.Web.Services.WebService
{
[WebMethod]
public void throwCustomException()
{
Trace.WriteLine("Throwing custom exception now.");
throw new MyCustomException();
}
} //End of class ExceptionThrower
public class MyCustomException: System.Exception
{
private string mDetail = "I am the best guy.";
public MyCustomException()
{
mDetail = "I am *still* the best guy.";
}
public MyCustomException(string lDetail)
{
this.Detail += ("\n" + lDetail);
}
public string Detail
{
get
{
return mDetail;
}
set
{
mDetail = value;
Trace.WriteLine("Setting the value of mDetail...");
}
}
}//End of class MyCustomException
}