我有一个类,其中有大约20个方法.每个人都做一些Web服务消息处理.我只需对其进行更改,并意识到这些方法中的每一个都具有完全相同的try/catch:
try
{
/* *** actual processing specific to each method goes here *** */
}
catch (FaultException<CustomException> cfex)
{
// common stuff
}
catch (CustomException cfex)
{
// common stuff
}
catch (Exception ex)
{
// common stuff
}
finally
{
FinalizeServiceCall(wsBus, wsMessage, response, logProps);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是; 而不是在每个方法中都有这个完全相同的try/catch块,有没有办法让它变得常见?我的想法是.NET有这样的东西TransactionScope以某种方式检测离开该块时是否发生异常.有没有我可以利用这样的东西来制作一个常见的try/catch块?还有其他想法吗?
我有以下代码,它在尝试抛出错误时给出了" 用户代码未处理的异常 ":
private static void _msgQ_RecieveCompleted(object sender, ReceiveCompletedEventArgs e)
{
try
{
//queue that have received a message
MessageQueue _mq = (MessageQueue)sender;
//get the message off the queue
Message _mqmsg = _mq.EndReceive(e.AsyncResult);
throw new Exception("This is a test exception by Tim");
//set the values back into a formatted struct
//now process your SQL....
Azure_SQL _azuresql = new Azure_SQL();
_azuresql.writeMessageToStorage((_TwitterStreamFeed)_mqmsg.Body);
//refresh queue just in case any changes occurred (optional)
_mq.Refresh();
//tell MessageQueue to receive next message when it arrives
_mq.BeginReceive(); …Run Code Online (Sandbox Code Playgroud)