0

i'm a newbie .net mvc developer and i want to know, is there anyway to develop my own error handler without using the famous try/catch statement. i'm using an XML file to render components in a view, and i want for example to get an error message useful for me to know that i violated a constraint based on the xml file

(example if i add another tag without >'type' attribute, this can generate a message for me at runtime). i hope i make myself clear thanks in advance,

3 Answers 3

1

In the Global.asax can catch the exceptions, global.asax has many handlers of events on your aplication like Application_start or that you need Application_Error

void Application_Error(){

     Exception exception = Server.GetLastError();
     //do what you need with the exception...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Actually i prefer using the ActionFilter class to override the OnActionExecuting, OnActionExecuted,OnResultExecuting and OnResultExecuted method. And i think that the control of xml properties cannot be done from c sharp, well i will use the if condition for that. Thanks anyway

Comments

0

I believe the best place for handling exceptions is in global.asax.cs.

protected void Application_Error(object sender, System.EventArgs e)
{
    // custom logger
    IErrorLogger errorLogger = new CustomLogger();

    // config of error handler
    MvcExceptionHandlerConfig config = new MvcExceptionHandlerConfig("Error", "Error", 404, 500);

    // init and handle
    new MvcExceptionHandler.MvcExceptionHandler(this, errorLogger, config).Handle();
}

Demo is available here: https://github.com/mholec/mvcexceptionhandler

Of course, it depends, what kind of exceptions you want to handle. Application_Error is great place for handling errors 400, 404, 5xx etc., on the other hand it is not usable for authentication errors (you need to create special filter).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.