如何在C#中获得像0x80131904这样的eccor代码 [英] How to get eccor code like 0x80131904 in C#

查看:108
本文介绍了如何在C#中获得像0x80131904这样的eccor代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#exception中找出错误代码,例如 - 0x80131904,

ex.Message

ex.InnerException

ex。来源

ex.StackTrace



但需要获取错误代码



我尝试过:



ex.Message

ex.InnerException

ex.Source

ex.StackTrace

I want to find out error code in c# exception , eg- 0x80131904,
ex.Message
ex.InnerException
ex.Source
ex.StackTrace

but need to get the error code

What I have tried:

ex.Message
ex.InnerException
ex.Source
ex.StackTrace

推荐答案

如果您有C#异常类(系统) [ ^ ]实例使用 Exception.HResult属性(系统) [ ^ ]获取错误代码。
If you have a C# Exception Class (System)[^] instance use the Exception.HResult Property (System)[^] to get the error code.

<无线电通信/>

异常没有错误代码 - 错误代码是一种更老,更不友好,不太灵活的方式来报告应用程序的问题。

在过去,应用程序要小得多,并且空间非常宝贵 - 因此错误被安装到一个紧凑的空间--32位 - 因此只是一个数字 - 为了以可理解的方式向用户呈现,使用查找表(通常来自文件)从错误代码中获取人类可读的字符串。



像C#这样的现代语言使用Exceptions,它是基于类的,包含人类可读(大多数)字符串,可以直接显示给用户。

异常有不同的类型:异常是基类,但你可以捕获SqlException,ApplicationException和一大堆其他分组异常,使你的代码更友好,更可读。



没有系统可以将异常转换为错误代码。
Exceptions don't have error codes - error codes are a much older, less friendly, less flexible way to report a problem with an application.
In the old days, apps were a lot smaller, and space was at a premium - so the error was fitted into a compact space - 32 bits - and as a result was "just a number" - in order to present that to the user in an understandable way, a lookup table (usually from a file) was used to get a human readable string from the error code.

Modern languages like C# work with Exceptions which are class based and contain human readable (mostly) strings which are ready for display to the user directly.
Exceptions come in different types: Exception is the base class, but you can catch SqlException, ApplicationException, and a whole host of other "grouped" exceptions to make your code both more friendly and more readable.

There is no system to convert an Exception to an error code.


这篇关于如何在C#中获得像0x80131904这样的eccor代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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