Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I throw an exception when returning objects?

Tags:

c#

exception

I have a web method like this as part of a web service

public List<CustomObject> GetInformation() {
    List<CustomObject> cc = new List<CustomObject>();

    // Execute a SQL command
    dr = SQL.Execute(sql);

    if (dr != null) {
       while(dr.Read()) {
           CustomObject c = new CustomObject()
           c.Key = dr[0].ToString();
           c.Value = dr[1].ToString();
           c.Meta = dr[2].ToString();
           cc.Add(c);
       }
    }
    return cc;
}

I want to incorporate error handling into this so that if the no rows are returned or if dr is null or if something else goes wrong, I want to return the exception in the form of an error description. However, if the function is returning List<CustomObject> how can I also return an error message to the client when something goes wrong?

like image 588
Legend Avatar asked Jun 30 '12 00:06

Legend


People also ask

Can you throw an exception and return something?

It's not possible to both throw an exception and return a value from a single function call. Perhaps it does something like returning false if there's an error, but throwing an exception if the input is invalid.

Do you need to return after throwing an exception?

After throwing an exception, you do not need to return because throw returns for you. Throwing will bubble up the call stack to the next exception handler so returning is not required.

How do I return an exception in Java?

You can throw an exception in Java by using the throw keyword. This action will cause an exception to be raised and will require the calling method to catch the exception or throw the exception to the next level in the call stack.

Should I return null or throw exception?

Only throw an exception if it is truly an error. If it is expected behavior for the object to not exist, return the null. Otherwise it is a matter of preference. It certainly shouldn't be a matter of preference.


1 Answers

I will create a wrapper class which has both your List of CustomObject and the error information as another Property.

public class MyCustomerInfo
{
  public List<CustomObject> CustomerList { set;get;}
  public string ErrorDetails { set;get;}

  public MyCustomerInfo()
  {
    if(CustomerList==null)
       CustomerList=new List<CustomObject>();  
  }
}

Now I will return an object of this class from my Method

public MyCustomerInfo GetCustomerDetails()
{
  var customerInfo=new MyCustomerInfo();

  // Execute a SQL command
    try
    {
      dr = SQL.Execute(sql);

      if(dr != null) {
         while(dr.Read()) {
           CustomObject c = new CustomObject();
           c.Key = dr[0].ToString();
           c.Value = dr[1].ToString();
           c.Meta = dr[2].ToString();
           customerInfo.CustomerList.Add(c);
         }
      }
      else
      {
          customerInfo.ErrorDetails="No records found";
      } 
   }
   catch(Exception ex)
   {
       //Log the error in this layer also if you need it.
       customerInfo.ErrorDetails=ex.Message;
   }     
  return customerInfo;    
}

EDIT: To make it more reusable and generic, It is a good idea to create a seperate class to handle this. and I will have this in my Baseclass as a property

public class OperationStatus
{
  public bool IsSuccess { set;get;}
  public string ErrorMessage { set;get;}
  public string ErrorCode { set;get;}
  public string InnerException { set;get;}
}
public class BaseEntity
{
  public OperationStatus OperationStatus {set;get;}
  public BaseEntity()
  {
      if(OperationStatus==null)
         OperationStatus=new OperationStatus();
  } 
}

And Let all your Child entities involved in a transaction inherit from this base class.

   public MyCustomInfo : BaseEntity
   {
      public List<CustomObject> CustomerList { set;get;}
      //Your constructor logic to initialize property values
   } 

Now from your Method you may set the values of OperationStatus property as needed

public MyCustomInfo GetThatInfo()
{
    var thatObject=new MyCustomInfo();
    try
    {
       //Do something 
       thatObject.OperationStatus.IsSuccess=true;
    }
    catch(Exception ex)
    {
      thatObject.OperationStatus.ErrorMessage=ex.Message;
      thatObject.OperationStatus.InnerException =(ex.InnerException!=null)?ex.InnerException:"";
    }
    return thatObject;
}
like image 82
Shyju Avatar answered Oct 06 '22 19:10

Shyju