Home > OS >  where the error coming from inside Try Catch
where the error coming from inside Try Catch

Time:01-17

I am doing 5 things inside a try/catch. If there is a error, how can i tell which function the error is coming from. so on line Console.WriteLine("Invalid operation"); I want it to display which function the error is so that its easy to debug.

I am trying to avoid using 5 different try/catch in Main OnGet() method. I was thinking to put try/catch inside each sub-method but I am return IQueryable so I wont be able to return error as type string. even so I will end up have 5 different if statement to check for error in OnGet()

How do you guys handle something like this?

   public async Task<IActionResult> OnGetAsync(int p = 1, int s = 20)
    {
        try
        {
            // get all Data 
            IQueryable<MY_Model> Query = await _services.Get_All_CoursesTaken();
            // Add Filters
            Query = AddFilters(Query );
            // Add Sorting
            Query = AddSorting(Query );
            // Add Paging
            Query = AddPaging(p, s, Query );
            //Display Data
            My_List= await Query.AsNoTracking().ToListAsync();
        }
        catch (InvalidOperationException)
        {
            Console.WriteLine("Invalid operation");
        }
  return Page();
}


 private IQueryable<MY_Model> AddFilters(IQueryable<MY_Model> Query)
    {
       //some code 
    }

 private IQueryable<MY_Model> AddSorting(IQueryable<MY_Model> Query)
    {
       //some code 
    }

 private IQueryable<MY_Model> AddPaging(int p, int s, IQueryable<MY_Model> Query)
    {
       //some code 
    }

CodePudding user response:

Maybe your solution is:

Tuple

You can return IQueryable and String as return type in methods.

   private Tuple<IQueryable<MY_Model>,string> AddFilters(IQueryable<MY_Model> Query)
  • Related