Tag: exception处理

有没有办法测试我的转换以避免使用exception?

我正在使用Reflection从DataRows构造任意对象,当橡胶最终遇到道路时,我需要从DataRow获取一个值并将其分配给对象上的属性。 由于DataRows可能包含不支持转换的类型,因此其中许多都会导致必须处理的exception。 例如,DBnulls可能会在DataRow中出现,或者某种数字格式不能很好地转换为另一种,等等。有什么办法可以避免抛出exception吗? (我不会接受巨大的switch语句来检查目标对象属性和源数据中的每种类型组合。) public void Merge(DataRow data) { PropertyInfo[] props = this.GetType().GetProperties(BindingFlags…); foreach (PropertyInfo pi in props.Where(p => T_IsPrimitive(p.PropertyType))) { object src = null; if( dataAsDataRow.Table.Columns.Contains(pi.Name) ) src = ((DataRow)data)[pi.Name]; if (src != null) { if( src.GetType() != pi.PropertyType ) { try { src = Convert.ChangeType(src, pi.PropertyType); } catch(InvalidCastException e) { try { src […]

当我使用命令行构建C#/ .NET时,如何设置配置?

当我的程序试图访问网络驱动器中的DLL时,我收到此错误消息。 Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly ‘file:///Z:\smcho\works\tasks\2011\ni\ng_fpgabackend\myclass.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) —> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This […]

log4net没有记录和抛出错误

当我的Web服务中有任何exception时,我一直在使用log4Net写入文本文件,这是有效的,但今天它开始抛出错误。 System.Web.Services.Protocols.SoapException: Server was unable to 处理请求。 —> System.TypeInitializationException:..的类型初始值设定项引发exception。 —> System.IO.FileLoadException:无法加载文件或程序集’log4net,Version = 1.2.9.0,Culture = neutral,PublicKeyToken = b32731d11ce58905’或其依赖项之一。 定位的程序集的清单定义与程序集引用不匹配。 (来自HRESULT的exception:0x80131040)文件名:’log4net,Version = 1.2.9.0,Culture = neutral,PublicKeyToken = b32731d11ce58905′ WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly […]

$ locile在“locals”中定义

H我们有很常用的日志实用程序函数,我想通过添加对Server.GetLastError()的调用来调整它,如果还有错误也要记录。 日志函数是单独项目的一部分,所以我尝试使用HttpContext.Current.Server.GetLastError() (正如我对Request,ServerVariables和Session属性所做的那样)。 在测试期间,我创建了简单的exception: int i=0, j=0; try { int k = i / j; } catch (Exception E) { Tools.CooLog(“in”); } Tools.CooLog(“out”); 为了找出“HttpContext.Current.Server.GetLastError()”是否会在Tools.CooLog(“out”);时返回exceptionTools.CooLog(“out”); 叫做。 相反,我有两个很大的惊喜1.在两个调用中, HttpContext.Current.Server.GetLastError()返回null。 2.也许最奇怪的是,在本地部分第一次调用CooLog期间,我看到了一些年轻,英俊和PHP版本 – 我看到有一个名为$ exception的值,令人惊讶的是HttpContext.Current.Server.GetLastError()的例外情况HttpContext.Current.Server.GetLastError()无法检索! 所以我的问题是1.为什么HttpContext.Current.Server.GetLastError()返回null? ( HttpContext.Current.Request.ServerVariables正常工作)2。$ exception来自何处? 有没有办法使用它? (在第二次调用CooLog时,变量未定义)

如何处理抛出外部dll的exception?

我开发了一个使用外部DLL作为FTPServer的项目,我在我的项目中创建了这样的FTP服务器: private ClsFTPServer _ClsFTPServer; _ClsFTPServer = new ClsFTPServer(FTPUserName, FTPPassword, FTPPath); 上面的代码创建了一个FTP服务器类的实例,该类在它的构造函数上启动FTPserver,它作为一个模块独立工作,而客户端正确发送它们的请求,但是当一个不正确的请求到达FTP服务器时它抛出exception并导致我的崩溃申请。 如何处理外部dll抛出的exception以防止我的应用程序崩溃?

在使用F5时,最终似乎没有在C#控制台应用程序中执行

int i=0; try{ int j = 10/i; } catch(IOException e){} finally{ Console.WriteLine(“In finally”); Console.ReadLine(); } 在VS2008中按F5时,似乎不执行finally块。 我在控制台应用程序中使用此代码。

检查Code中的CustomErrors已打开

是否可以在Web应用程序运行时的代码中检查天气自定义错误是打开还是关闭。

两次序列化XML相同的标记

我有问题: 我必须将类序列化为XML文件。 但是必须使用相同的名称命名两个属性: 期望的xml: AAA BBB 我不需要将它反序列化回对象。 码: public class Header1 { [XmlElement(“Tag1”)] public String Tag1 { get; set; } } public class Header2 { [XmlElement(“Tag2”)] public String Tag2 { get; set; } } public class Test { [XmlElement(“HeaderText”)] public Header1 Header1 { get; set; } [XmlElement(“HeaderText”)] public Header2 Header2 { get; set; } } […]

XML序列化错误:2种类型都使用XML类型名称“关系”,来自命名空间”

我在通过XML序列化时遇到问题,因为2个clases使用一个名为Relationship的类(尽管是不同的类!)。 我尝试使用XML属性用另一个名称装饰其中一个类,但它仍然给我以下错误: {“Types’SiteServer.Relationship’和’LocalServer.Relationship’都使用XML类型名称’Relationship’,来自命名空间”。使用XML属性为类型指定唯一的XML名称和/或命名空间。”} 这是我的2个class,有谁知道为什么? 我使用错误的属性? 它似乎忽略了它:-) public class SiteServer { [XmlRoot(“SiteServerRelationShip”)] public class Relationship { public string type { get; set; } } public string Name { get; set; } public Relationship Relate = new Relationship(); } public class LocalServer { public class Relationship { public string type { get; set; } } public string […]

是否有适合使用空挡块的情况?

可能重复: 为什么空捕获阻止了一个坏主意? 是否有任何有效理由忽略捕获的exception 你知道任何情况下空的挡块不是绝对的邪恶吗? try { … // What and When? … } catch { }