tags:

views:

133

answers:

4

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.Feedback.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.");
  }
}
+5  A: 

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.

Brian R. Bondy
+2  A: 

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).

Mehrdad Afshari
A: 

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

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

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

Ian Oxley