Tag: 例外

识别相同OleDbException类型的exception

我有以下代码来validation另一个应用程序(数据库已经有密码)是否在独占模式下打开MSAccess 2003数据库: OleDbConnectionStringBuilder conString = new OleDbConnectionStringBuilder(); conString.Provider = “Microsoft.Jet.OLEDB.4.0”; conString.DataSource = “some path to some mdb file”; // I don’t care about the password, // I just whant to know if it is opened in Exclusive Mode conString[“Jet OLEDB:Database Password”] = String.Empty; conString[“Mode”] = “Share Deny None”; string completeConnStr = conString.ConnectionString; using (OleDbConnection con […]

C#:应该捕获所有exception

是否应该在C#程序中捕获所有exception,或者是一些例外(例如堆栈溢出,内存不足等),您应该允许程序崩溃,因为无法从它们中恢复?

当任何数学运算在.net 4中产生’NaN’时,如何强制C#编译器抛出exception?

我有一个很复杂的源代码,我需要找到变量值设置为nan的确切位置。 所以我需要编译器在那时抛出一个exception。 之前已经问过这个问题。 我发现以下答案是一个很好的答案。 此代码在.net 3.5中运行良好。但是当我使用.net 4时,此解决方案无法正常工作。 即使我在调试器中启用“抛出exception时中断”,也无法在代码中找到exception。 任何的想法? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace ConsoleApplication2 { class Program { [System.Runtime.InteropServices.DllImport(“msvcrt.dll”)] public static extern uint _control87(uint a, uint b); [System.Runtime.InteropServices.DllImport(“msvcrt.dll”)] public static extern uint _clearfp(); static void Main(string[] args) { float zero = 0.0f – args.Length; // Want […]

从AppDomain.AssemblyLoad事件中抛出exception

有人可以向我解释为什么我似乎无法从AppDomain.Assembly加载事件中抛出exception? 例如: class Program { static Program() { AppDomain.CurrentDomain.UnhandledException += (s, a) => { Console.WriteLine(“Caught exception!”); }; AppDomain.CurrentDomain.AssemblyLoad += (s, a) => { Console.WriteLine(string.Format(“Assembly {0} loaded”, a.LoadedAssembly.FullName)); throw new Exception(); Console.WriteLine(“Should never get here…”); }; } static void Main(string[] args) { Console.WriteLine(new ClassLibrary1.Class1().TestString()); Console.WriteLine(); Console.WriteLine(“Done…”); Console.ReadLine(); } } 当我执行它时,输出如下: Assembly ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null loaded […]

捕获exceptionC#

什么是正确的方法。 捕获从最具体到最普遍或相反的exception。 如果我写 try { … } catch( Exception e ) { … } catch( NullReferenceException nre ) { … } NullReferenceException是否会被捕获?

C#中的TypeInitializationException

我将基于教程创建学生信息系统。 只要用户想要将新学生添加到数据库,就会发生以下exception。 我试着学习一些关于TypeInitializationException的东西,我对它的名字有点了解……但是我无法完全理解它。 此外,我正在遵循的教程中没有例外。我是这种类型的OOP编程和处理错误的新手。 我在下面包含了我的数据库连接和数据库访问类。 我的DB_Connection类 using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace studentInformationSystem { class DB_Connection { public static SqlConnection NewConnection; public static string ConString = ConfigurationManager.ConnectionStrings[“ConString”].ConnectionString; public static SqlConnection getConnection() { NewConnection = new SqlConnection(ConString); return NewConnection; } } } 我的DB_Access类 using System; using System.Collections.Generic; using System.Linq; […]

如何处理exception消息的翻译?

我真的不知道在多语言应用程序中处理exception的最佳方法。 我应该在哪里处理错误消息的翻译( Exception.Message )? 如果我抛出exception,我应该在ctor中翻译消息吗? throw new MyException(“Error message”, Resource.MyException_TranslatedMessage); 或者我抛出exception并使用自制帮助程序,它将使用View逻辑中的exception类型找到错误消息? try { //… } catch(Exception ex) { myLabel.Text = new ExceptionTranslator(ex).Translate(); } 或者,Microsoft是否提供了这样做的工具或机制? 总之一句:处理exception消息翻译的好方法是什么?

抛出未处理的开关盒的正确例外是什么?

可能重复: C#:当没有指定的大小写可以处理时,在switch语句中抛出exception 假设我们有SomeEnum并且有一个switch语句来处理它: enum SomeEnum { One, Two } void someFunc(SomeEnum value) { switch(value) { case One: … break; case Two: … break; default: throw new ??????Exception(“Unhandled value: ” + value.ToString()); } } 如您所见,我们处理所有可能的枚举值,但仍保留默认值,以防添加新成员,并且我们希望确保我们知道丢失的处理。 我的问题是:在你想要通知给定的代码路径没有被处理/实现或者从未被访问过的情况下,什么是正确的exception? 我们曾经使用过NotImplementedException但它似乎不合适。 我们的下一个候选者是InvalidOperationException但该术语听起来不正确。 什么是正确的,为什么?

如何在C#中处理特定的SQLexception(例如,唯一约束违规)?

我的问题是如何在c#中处理sqlexception,无论如何都要检查从数据访问层抛出什么样的sqlexception? 例如,如果db抛出唯一约束exception或外键exception,有没有办法从c#中捕获它? 你用于这些数据库exception的exception处理模式是什么?

表明服务器请求失败的最佳实践方法?

我正在编写一个连接到服务的API,该服务要么返回简单的“成功”消息,要么返回100多种不同类型的失败之一。 最初我想写一个向这个服务发送请求的方法,如果它成功了,那么该方法什么都不返回,但如果它因任何原因失败,它会抛出一个exception。 我不太介意这个设计,但另一方面,就在今天,我正在阅读Joshua Bloch的“ 如何设计一个好的API以及它为何重要 ”,他在那里说“抛出exception以表明特殊条件…… Don’ t强制客户端使用控制流的exception。“ (和“相反,不要默默地失败。”) 另一方面,我注意到我正在使用的HttpWebRequest似乎在请求失败时抛出exception,而不是返回包含“500 Internal Server Error”消息的Response。 在这种情况下报告错误的最佳模式是什么? 如果我在每次失败的请求中抛出exception,那么我将来会在某些时候感受到巨大的痛苦吗? 编辑:非常感谢您对目前为止的回复。 一些细化: 它是一个DLL,将提供给客户端在他们的应用程序中引用。 使用的类似例子是ChargeCreditCard(CreditCardInfo i) – 显然当ChargeCreditCard()方法失败时,它是一个巨大的交易; 我只是不确定是否应该停止印刷机或将责任传递给客户。 编辑第二:基本上我并不完全相信使用这两种方法中的哪一种: try { ChargeCreditCard(cardNumber, expDate, hugeAmountOMoney); } catch(ChargeFailException e) { // client handles error depending on type of failure as specified by specific type of exception } 要么 var status = TryChargeCreditCard(cardNumber, […]