Handle Error Attribute at Global Level

Monday, 17 February 2014

Handle Error Attribute at Global Level


Controller:

   public class GlobalOverflowExceptionController : Controller
    {
        //
        // GET: /GlobalOverflowException/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult overflowexception()
        {

            int value = int.MaxValue + int.Parse("1");

            if (value < 0)
            {
                throw new OverflowException();

            }
            else
            {
                ViewBag.Result = value;
            }
            return View();
        }

        public ActionResult dividebyzeroexception()
        {


            int a = 10;
            int b = 0;
            int c = a / b;
            return View();
        }

        public ActionResult overflowexception1()
        {

            int value = int.MaxValue + int.Parse("1");

            if (value < 0)
            {
                throw new OverflowException();

            }
            else
            {
                ViewBag.Result = value;
            }
            return View();
        }

    }

HandleError Attribute at Global Level:

 

 

 

View:

overflowexception:

@{
    ViewBag.Title = "overflowexception";
}

<h2>overflowexception</h2>
<h2>@ViewBag.Result</h2>

 

Screenshot:

 

 

 

Comment overflowexception filter 

 

 Screenshot:

 

 

 Conclusion:

     Catch the overflowexception at application level.

HandleErrorAttribute at controller level


HandleErrorAttribute at controller level

Controller:

    [HandleError(ExceptionType = typeof(System.OverflowException), View = "overflowexception")]
    public class OverflowExceptionController : Controller
    {
        //
        // GET: /OverflowException/

        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult overflowexception()
        {

            int value = int.MaxValue + int.Parse("1");

            if (value < 0)
            {
                throw new OverflowException();

            }
            else
            {
                ViewBag.Result = value;
            }
            return View();
        }
    
        public ActionResult dividebyzeroexception()
        {


            int a = 10;
            int b = 0;
            int c = a / b;
            return View();
        }
      
        public ActionResult overflowexception1()
        {

            int value = int.MaxValue + int.Parse("1");

            if (value < 0)
            {
                throw new OverflowException();

            }
            else
            {
                ViewBag.Result = value;
            }
            return View();
        }
    }

View:

@{
    ViewBag.Title = "overflowexception";
}

<h2>overflowexception</h2>
<h2>@ViewBag.Result</h2>


  

Screenshot:




Conclusion:

  Created the common overflowexception error page for multiple actions in multiple controller









Handle Error Attribute at ActionLevel

Handle Error Attribute at ActionLevel

Description:

  •   Here,Used the handleerrorattribute at action level. 
  •  ExceptionType property of HandleError: Catch the specify error at action level.
  •  View property of HandleError:Display the view page,If occur specify error of executiontypeproperty in Action level.
  • Create view only for overflowexception

Controller:

     

       [HandleError(ExceptionType = typeof(System.OverflowException), View = "overflowexception")]
        public ActionResult overflowexception()
        {
           
            int value = int.MaxValue + int.Parse("1");
         
            if (value < 0)
            {
                throw new OverflowException();
              
            }
            else
            {
                ViewBag.Result = value;
            }
            return View();
        }
        [HandleError(ExceptionType = typeof(System.OverflowException), View = "overflowexception")]
        public ActionResult dividebyzeroexception()
        {
         
          
            int a = 10;
            int b = 0;
            int c = a / b;
            return View();
        }
        [HandleError(ExceptionType = typeof(System.OverflowException), View = "overflowexception")]
        public ActionResult overflowexception1()
        {
           
            int value = int.MaxValue + int.Parse("1");

            if (value < 0)
            {
                throw new OverflowException();

            }
            else
            {
                ViewBag.Result = value;
            }
            return View();
        }

------------------------------------------------------------------------------------------------------------

View:

overflowexception.cshtml:

  @{
    ViewBag.Title = "overflowexception";
}

<h2>overflowexception</h2>
<h2>@ViewBag.Result</h2>

-------------------------------------------------------------------------------------------------------------



ScreenShot:

 Execution path:/Home/overflowexception

 Throw the error Overflowexception and Display the overflowexception view.
   

UI:




Execution path:Home/dividebyzeroexception

Error occur as Dividebyzeroexception and not display the overflowexception view.Because,specify the handle error for overflowexception only.



UI:



    Execution path:/Home/overflowexception1








UI:





-----------------------------------------------------------------------------------------------------------

Conclusion:

   Created the new page for display the overflowexception .