前言:本人对异常的捕获是个初学者,今天刚有所接触,做个简要记录,以后会逐步完善,不足之处请批评指正:
项目中使用C#代码直接抛出一个异常:
throw new ArgumentException("错误的压缩级别");
A-代码捕捉异常处理:.net Web项目,Global项目启动页,项目异常处理代码:
protected void Application_Error(object sender, EventArgs e) { if (this.Request.FilePath == "/") { this.Server.ClearError(); return; } Exception error = this.Server.GetLastError(); Exception exception = error.InnerException ?? error; if (exception is HttpException) { if ((exception as HttpException).GetHttpCode() == HttpStatusCode.NotFound.GetValue()) { return; } } _Log.Error(" Client IP:" + this.Request.UserHostAddress + " Error Page:" + this.Request.Url, exception); HttpContext context = this.Context; string userName = context.User == null ? "未登录用户" : context.User.Identity.Name; string referrer = Request.UrlReferrer == null ? string.Empty : this.Request.UrlReferrer.AbsoluteUri; //this.Context.Items.Add("exception", exception);// <customErrors redirectMode="ResponseRewrite" defaultRedirect="~/404.html" mode="On"> }
B:配置文件项目异常404跳转处理:.net Web项目,web.config配置异常捕获:
<httpErrors> <remove statusCode="404" subStatusCode="-1" /> <error statusCode="404" prefixLanguageFilePath="" path="/404.html" responseMode="ExecuteURL" /> <error statusCode="404" path="/404.html" responseMode="ExecuteURL" subStatusCode="1" /> <error statusCode="404" path="/fr/404.html" responseMode="ExecuteURL" subStatusCode="2" /> <error statusCode="404" path="/es/404.html" responseMode="ExecuteURL" subStatusCode="3" /> <error statusCode="404" path="/ja/404.html" responseMode="ExecuteURL" subStatusCode="4" /> <error statusCode="404" path="/ar/404.html" responseMode="ExecuteURL" subStatusCode="5" /> <error statusCode="404" path="/de/404.html" responseMode="ExecuteURL" subStatusCode="6" /> <error statusCode="404" path="/it/404.html" responseMode="ExecuteURL" subStatusCode="7" /> </httpErrors>