开发者

in C# try -catch , can't catch the exception

below code can't catch the exception.

does catch can't catch the exception which occured in the function?

 try
 {
   Arche.Members.Feedback.FeedbackBiz_Tx a = 
     new Arche.Members.Feed开发者_StackOverflowback.FeedbackBiz_Tx();

   a.AddFreeSubscriptionMember(
     itemNo, buyerID, itemName, 
     DateTime.Today,DateTime.Today);
 }
 catch(Exception ex)
 {
   RegisterAlertScript(ex.Message);
 }

...

public void AddFreeSubscriptionMember(
  string itemNo, string buyerID, string itemName,
  DateTime fsStartDate, DateTime fsEndDate)
{
  FeedbackBiz_NTx bizNTx = new FeedbackBiz_NTx();

  if (bizNTx.ExistFreeSubscription(buyerID, itemNo))
  {
    throw new Exception("Exception.");
  }
}


Yes it will catch the exception even know it is thrown from within another function that you are calling.

Either the exception isn't being thrown, or you aren't detecting properly that the exception is caught, maybe put a breakpoint at both places.


If the ExistFreeSubscription function causes a stack overflow, it won't be caught.

Also, it's possible for the function to throw an exception object that doesn't inherit from System.Exception (this is possible in other languages, it's not CLS compliant). catch (Exception ex) won't catch such kind of exceptions (a catch { } block can catch exception objects even if they are not inherited from System.Exception).


That should work, I would look closer at your .ExistFreeSubscription() method, to see why it's not returning true.


if (bizNTx.ExistFreeSubscription(buyerID, itemNo))
{
    throw new Exception("Exception.");
}

If bizNTx.ExistFreeSubscription returns false, then it looks like your Exception won't be thrown

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜