0

I have SVC services running in the Asp.net hosted with IIS 7.5, When the exception occurs it is not throwing the exception and continues to execute the next line!

I am specificaly throwing the error as below when condition not met or for example data not found.

throw new Exception("Data Not Found");

My code is as below

  using System;

  public class DataProvider : IDataProvider
  {
    private AccountData _accountData;
    private OwnerData _ownerData;

  public DataProvider()
  {
    var dataAccessor = new DataAccessor();

    _accountData = dataAccessor.AccountDetails();

    //throw the error in constructor 
    if (_accountData == null)
        throw new Exception("Data Not Found");

    if (_accountData.AccountInfo != null)
    {
        _ownerData = _accountData.AccountInfo.OwnerData;
        //continue....
    }
  }

  public string GetAccountOwner()
  {
    if (_ownerData != null)
    {
        return _ownerData.Name;
    }

    return null;
  }
}

Main Method:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class AccountService : IAccountService
{
  private readonly IDataProvider _dataProvider;

 public AccountService()
    : this(new DataProvider())
 {
 }

 public AccountService(IDataProvider dataProvider)
    {
        _dataProvider;= dataProvider;
    }

 public AccountResponse GetOwner()
 {
    try
    {
        var owner = _dataProvider.GetAccountOwner();

        return new AccountResponse
        {
            Owner = owner
        };
    }
    catch (Exception ex)
    {
        if (ex.Message == "Data Not Found")
        {
            //do something here...
        }

        return null;
    }
}

}

updev
  • 623
  • 5
  • 14
  • 32

0 Answers0