当C#中存在内部服务器错误(代码:500)时,您能建议抛出什么异常吗? [英] Can you suggest what exception to throw when there is an internal server error (code:500) in C#?

查看:502
本文介绍了当C#中存在内部服务器错误(代码:500)时,您能建议抛出什么异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我收到代码时,我想抛出一个像WebException这样的常规异常:来自C#中HTTP响应的500内部服务器错误。你能告诉我哪个例外吗?



我尝试了什么:



我编写了一个内部服务器异常类,但要求是抛出一些常规异常。

I want to throw a general exception like "WebException" whenever i receive a code : 500 internal server error from a HTTP Response in C#. Can you please suggest me which exception to throw?

What I have tried:

I have written a Internal Server Exception Class but the requirement is to throw some general exception.

推荐答案

我个人赞成建立一个内部服务器错误的特定异常类,特别是如果这段代码会暂停一段时间,但如果这只是一个练习,或者它是一次性的,那么通用的抛出新的异常(< messagehere> );工作正常并满足要求
Personally, I'm all in favor of building a specific exception class for an internal server error, especially if this code is going to hang around for a while, but if this is just an exercise, or it's a throw-away one off, then a generic "throw new Exception(<messagehere>);" works just fine and fulfills the requirement


嗯,HTTP状态代码(包括500)应该作为响应的一部分从服务器发送到客户端,无论哪个平台客户端是由。然而,异常特定于运行时环境,使用该环境构建服务器代码。此外,异常不会传播到客户端,而是记录并在严重的情况下通知系统管理员。因此,在我看来,应该有特定的异常类而不是一个通用的异常类。此外,这些异常类应表示引发异常的应用层(例如DA层,业务服务,数据库)。这将有助于在现场快速识别和解决问题。
Well, HTTP status codes (including 500) are supposed to be sent across the wire from server to the client as part of a response, no matter which platform the client is built from. Whereas, the exceptions are specific to the run-time environment, using which the server code is built. Further, the exceptions are not propagated till the client, but rather logged and in severe condition, notified to the system administrator. Hence, to my opinion, there should be specific exception classes instead of one general exception class. Further, these exception classes should represent in which application layer (e.g. DA layer, business service, Database) the exception is raised. This will help identify and troubleshoot the issues quickly behind the scene.


这篇关于当C#中存在内部服务器错误(代码:500)时,您能建议抛出什么异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆