如何在C#中的异常中获取错误编号而不是错误消息? [英] How can i get the error number instead of error message in an Exception in C#?

查看:633
本文介绍了如何在C#中的异常中获取错误编号而不是错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可以得到相应的错误代码异常
我需要抛出异常错误代码而不是其消息,所以我基于错误代码我向用户显示正确的消息。

解决方案

如果您正在寻找win32错误代码,可以在 Win32Exception class

  {
Console.WriteLine(ErrorCode:{0},e.ErrorCode);
}

对于普通的老CLR异常,没有整数错误代码。 >

鉴于您所描述的问题,我将与 millimoose 的解决方案一起获取每种类型异常的资源字符串。


Is there a way that i can get the corresponding error code of an Exceptions ? I need the thrown exceptions error code instead of its message , so that i based on the error code i show the right message to the user.

解决方案

If you're looking for the win32 error code, that's available on the Win32Exception class

catch (Win32Exception e)
{  
    Console.WriteLine("ErrorCode: {0}", e.ErrorCode);
}

For plain old CLR exception, there is no integer error code.

Given the problem you describe, I'd go with millimoose's solution for getting resource strings for each type of exception.

这篇关于如何在C#中的异常中获取错误编号而不是错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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