-1

I am using custom exception for my application developing in MVC. I am using below link to use understand how can I handle custom exception.

Custom Exception Link of msdn

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace epay.Services.ExceptionClasses
{
    public class InvalidInputException : Exception
    {

    public InvalidInputException()
    {

    }

    public InvalidInputException(string message) : base(message)
    {

    }

    public InvalidInputException(string message, Exception inner) : base(message, inner)
    {

    }

    }
}

But I am completely confused how to use the these constructor which have message and inner exception as a parameter.

I have code in controller like below...

 [HttpPost]
        public ActionResult Create(PartyVM PartyVM)
        {
            try
            {
                PartyService partyService = new PartyService();
                var i = partyService.Insert(PartyVM);
                return RedirectToAction("SaveData", PartyVM);

            }
            catch (InvalidInputExceptione e)
            {
                CommonMethod commonMethod = new CommonMethod();
                PartyVM.AccountTypes = commonMethod.GetAccountTypes();

                TempData["error"] = e.Message.ToString() + "Error Message";
                return View("Create", PartyVM);
            }
}
bnil
  • 1,531
  • 6
  • 35
  • 68
  • 1
    Confused about what? P.s. e.Message is string so you don't need convert to string – Renatas M. Oct 06 '14 at 11:42
  • How to use the exception with constructor parameters ? – bnil Oct 06 '14 at 11:44
  • It seems to be correctly declared at least. Just remember to also add the protected constructor taking the serialization parameters, which is notoriously missing from your link too. See this too: http://stackoverflow.com/a/100369/2557263 – Alejandro Oct 06 '14 at 11:53

1 Answers1

1

Exceptions are constructed before they are thrown. The code you shared catches an exception.

You can throw an exception as follows:

throw new InvalidInputException("Invalid value for username");

The InnerException property is used when an exception was caught, but you want to wrap it in your own exception to provide more accurate exception information, for example this validates a string value for "Age":

public static class Validation
{
    public void ThrowIfAgeInvalid(string ageStr)
    {
        int age;
        try
        {
            // Should use int.TryParse() here, I know :)
            age = int.Parse(ageStr);
        }
        catch (Exception ex)
        {
            // An InnerException which originates to int.Parse
            throw new InvalidInputException("Please supply a numeric value for 'age'", ex);
        }

        if (age < 0 || age > 150)
        {
            // No InnerException because the exception originates to our code
            throw new InvalidInputException("Please provide a reasonable value for 'age'");
        }
    }
}

This way while debugging you still have a reference to the original cause of the problem.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • @user1650894 I did not mean to provide copy-paste ready code. The bottom line is, _you_ have to `throw` the exception or it will never appear. – C.Evenhuis Oct 06 '14 at 11:47
  • Not copy pasting , but where to write throw code ? In exception class or in controller's code ? – bnil Oct 06 '14 at 11:48
  • The exception should be thrown as soon as you find out the input is invalid. I'll try to provide another example. – C.Evenhuis Oct 06 '14 at 11:51
  • Sorry, but still not getting ... what I have to type in exception class constructors ? – bnil Oct 06 '14 at 11:52
  • Your `InvalidInputException` class looks fine. You don't need to do anything because `message` and `inner` are already relayed to the base class, where they will be accessible via the `Message` and `InnerException` properties. – C.Evenhuis Oct 06 '14 at 12:01