服务抛出的FaultException()异常未被客户端捕获catch(FaultException)
好吧,我知道我在这里遗漏了一些东西。我有以下操作合同:
public double DivideByZero(int x, int y)
{
if (y == 0)
{
throw new FaultException<ArgumentException>
(new ArgumentException("Just some dummy exception")
,new FaultReason("some very bogus reason"), new FaultCode("007"));
}
return x / y;
}
以下内容取自客户端:-
Console.WriteLine("Enter the x value");
string x = Console.ReadLine();
Console.WriteLine("Enter the Y value");
string y = Console.ReadLine();
try
{
double val = client.DivideByZero(Convert.ToInt32(x), Convert.ToInt32(y));
Console.WriteLine("The result is " + val.ToString());
}
catch(FaultException<ArgumentException> exp)
{
Console.WriteLine("An ArgumentException was thrown by the service "+ exp.ToString());
}
catch (Exception exp)
{
Console.WriteLine(exp.ToString());
}
在上述情况下,catch(FaultException exp)(客户端代码中带有 ArgumentException 的第一个 catch 块)块不会被执行。但是,当我删除 ArgumentException 以获取 catch(FaultException exp) 时,会执行相同的 catch 块。我对此不确定,因为我从操作合同中抛出了FaultException。我在这里错过了什么吗?
感谢您的帮助, Ashish
编辑:- 当我更新客户端中的服务引用时,我能够捕获 FaultException
异常。
Ok, I know I am missing something here. I have the following operation contract:
public double DivideByZero(int x, int y)
{
if (y == 0)
{
throw new FaultException<ArgumentException>
(new ArgumentException("Just some dummy exception")
,new FaultReason("some very bogus reason"), new FaultCode("007"));
}
return x / y;
}
And following is taken from the client:-
Console.WriteLine("Enter the x value");
string x = Console.ReadLine();
Console.WriteLine("Enter the Y value");
string y = Console.ReadLine();
try
{
double val = client.DivideByZero(Convert.ToInt32(x), Convert.ToInt32(y));
Console.WriteLine("The result is " + val.ToString());
}
catch(FaultException<ArgumentException> exp)
{
Console.WriteLine("An ArgumentException was thrown by the service "+ exp.ToString());
}
catch (Exception exp)
{
Console.WriteLine(exp.ToString());
}
In the above case catch(FaultException exp) (the first catch block with ArgumentException in the client code) block does not get executed. However, when I remove ArgumentException to have catch(FaultException exp), the same catch block gets executed. I am not sure about this as I am throwing FaultException from my operation contract. Am I missing anything here.
Appreciate your help,
Ashish
EDIT :- When I updated the service reference in my client, I was able to catch the FaultException<ArgumentException>
exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于某些任意数据协定类型,请尝试使用
FaultException
。如果您查看代理类中错误的生成代码,我打赌您会发现ArgumentException
没有按照您期望的方式进行序列化。Try it with
FaultException<DataContract>
for some arbitrary data contract type. If you look at the generated code for the fault in the proxy class, I bet you'll see that theArgumentException
is not serializing the way you expect it to.如果您要派生 Exception 类,请确保您的自定义异常具有序列化构造函数
If you are deriving Exception class, make sure that your custom exception has the serialization constructor