Avatar of ksd123
ksd123

asked on 

Using throw in C#

I am building an ASP.NET MVC application, and I am trying to find a way where I can collect  custom error messages if any and send  them back to the view.We have a employee class with id, firstname,lastname and designation properties  and  have a crew manager class with list of employees (IEnumarable) and other properties in it.I have following code that loops through each employee in crewmanager class and  calls ValidateMethod1 and throws custom error message to UI for any particlular employee .This code is working fine without any issue .But now I want to collect error messages for all the employees in crewmanager class and then send to UI. For example Suppose if  I have 10 employess in crewmanager  and below condition is satisfied for 7 employees it should send all the  7 employee's error messages (List) to UI. How can I  acheive this with below code?

foreach (var emp in CrewManager.Employees)
{
    ValidateMethod1(emp.id);
}

 Void ValidateMethod1(emp.id)
  {
        if(condition)
        {
            throw new CustomException("Exception with parameter employee '{0}', emp.id)
        }
 }

Open in new window

C#.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
ksd123
Avatar of Fareed Ali Khan
Fareed Ali Khan
Flag of Australia image

1) On controller level you can do by overriding the OnException method:

protected override void OnException(ExceptionContext filterContext)
{
    Exception e = filterContext.Exception;
    //Log Exception e
    filterContext.ExceptionHandled=true;
    filterContext.Result = new ViewResult()
    {
        ViewName = "Error"
    };
}

and forward it to Error view if you have any or log into some logging mechanism like eventlog or etc.

2) Global level exception handling. To achieve this you can use the following link:

http://www.codeproject.com/Articles/731913/Exception-Handling-in-MVC
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ksd123
ksd123

ASKER

Thank you.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo