Sungguk Lim
Sungguk Lim

Reputation: 6218

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.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.");
  }
}

Upvotes: 1

Views: 5331

Answers (4)

Ian Oxley
Ian Oxley

Reputation: 11056

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

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

Upvotes: 0

JohnForDummies
JohnForDummies

Reputation: 980

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

Upvotes: 0

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

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

Upvotes: 2

Brian R. Bondy
Brian R. Bondy

Reputation: 347216

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.

Upvotes: 5

Related Questions