Tag: exception

未发生C#WinForms应用程序中的预期跨线程exception

我来自WPF,我是WinForms的新手。 在研究跨线程情况时,我预计不会发生跨线程exception。 以下是我的情况的基本摘要。 有一个名为label1 Label控件和一个名为button1 Button 。 button1的click事件处理程序基本上如下所示: private void button1_Click(object sender, EventArgs e) { Task.Factory.StartNew(()=> { label1.Text = “Some-Other-New-Text”; }); } 这不会像我期望的那样抛出跨线程exception。 这是因为WinForms应用程序没有交叉线程问题吗? 请注意,我在Visual Studio 2013以及Visual Studio 2010中对此进行了调查。

ac #dll将错误返回给调用应用程序的好方法是什么?

我正在编写一个dll,它是访问数据库的包装器。 我一般都是c#的新手,因为我的背景是使用perl进行Web开发LAMP,我不知道什么是将错误返回给调用应用程序的好方法,以防它们将错误的参数传递给我的函数或者什么不是。 我现在不知道除了可能做一些msgbox或抛出一些例外,但我不知道从哪里开始寻找。 任何帮助或资源都将是有用的:) 谢谢〜

尝试NHibernate事务时捕获SqlException

我需要看到一个由SqlException产生的错误代码 – 但是,我无法触发它。 我使用NHibernate并在我的表上设置了SQL UNIQUE CONSTRAINT 。 当违反该约束时,我需要捕获错误代码并根据该消息生成用户友好的消息。 这是我的try / catch示例: using (var txn = NHibernateSession.Current.BeginTransaction()) { try { Session[“Report”] = report; _reportRepository.SaveOrUpdate(report); txn.Commit(); Fetch(null, report.ReportId, string.Empty); } catch (SqlException sqlE) { var test = sqlE.ErrorCode; ModelState.AddModelError(“name”, Constants.ErrorMessages.InvalidReportName); return Fetch(report, report.ReportId, true.ToString()); } catch (InvalidStateException ex) { txn.Rollback(); ModelState.AddModelErrorsFrom(ex, “report.”); } catch (Exception e) { […]

使用gson:MalformedJsonException解析Json File

我想读一个像JSON-File一样的字符串并在屏幕上打印。 因此我使用的是GSON-Library。 每次我想编译时,我在线程“main”com.google.gson.JsonSyntaxException错误中得到一个exception 我的代码看起来像这样 public class Test { public static void main(String… args) throws Exception { String json = “{” + “‘tag_name’ : ‘M mit Mbrunnen’,” + “‘tag_id’ : ‘de_bw_xx_mstall’,” + “‘tag_description’: ‘false’,” + “‘tag_latitude’: ‘42.704895’,” + “‘tag_longitude’: ‘10.652187’,” + “‘tag_description_f_a’: ‘Ein weiteres Highlight in H’,” + “}”; // Now do the magic. Data data […]

将本地语言exception转换为英语exception,包括英语堆栈跟踪

Currentlly我正在使用这种方法将exception转换为将原始exception消息转换为英语的exception: C#: [DebuggerStepThrough()] [Extension()] public Exception ToEnglish(T ex) where T : Exception { CultureInfo oldCI = Thread.CurrentThread.CurrentUICulture; Exception exEng = default(Exception); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(“en-US”); // Instance Exception one time to retrieve an English message. exEng = (Exception)Activator.CreateInstance(ex.GetType); // Instance Exception a second time to pass additional parameters to its constructor. exEng = (Exception)Activator.CreateInstance(ex.GetType, new object[] […]

例外与反思

Web服务具有SoapExtension,它包含error handling程序和xml格式的序列化错误。 Exception text. 如何制作error handling程序,调用“类型”错误? 例如: Type _type = Type.GetType(doc.DocumentElement.Attributes[“Type”].Value); 必须调用NullReferenceException。

C#DataContractSerializer序列化exception,在对象字段中设置了枚举

鉴于以下代码, [DataContract] public class TestClass { [DataMember] public object _TestVariable; public TestClass(object value) { _TestVariable = value; } public void Save() { using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(new FileStream(“test.tmp”, FileMode.Create))) { DataContractSerializer ser = new DataContractSerializer(typeof(TestClass)); ser.WriteObject(writer, this); } } } public enum MyEnum { One, Two, Three } 当_TestVariable设置为Enum值时,为什么无法序列化? new TestClass(1).Save(); // Works new TestClass(“asdfqwer”).Save(); […]

在新的子集合中枚举时,集合被修改了exception

我知道在你枚举的同时更改集合会导致collection was modified exception 。 但是如果我从较大的子集中得到一个子集合,并且我在枚举该子列表时从一个较大的子集删除了一些项目,我仍然会收到此错误。 在子集合上调用ToList解决了此问题。 但为什么会这样呢? var localCollection = someData.ToList(); // from DB Context var localGrouped = localCollection.GroupBy(x => x.Id).Select(g => new { Id = g.Key, List = g.Select(x => x.Value) }); or .ToList(); // Here how I solve exception var groups = new List<List>(); while (localGrouped.Any()) { var newSelected = new List(); […]

是否可以在C#类中为多个计时器设置一个Exception处理程序?

我有一个C#程序,它运行一个系统托盘应用程序 – 在后台传输/移动/创建/编辑文件。 如果用户手册删除了程序正在使用的文件,则有很多exception处理和循环处理可以防止程序崩溃/卡住。 不幸的是,程序运行的计算机之一是程序崩溃。 计算机很难获得,并且无法加载调试软件(它是一台没有网络访问权限的嵌入式PC)。 我已经尝试找到崩溃的原因(例如未处理的exeption)但是没有找到任何东西,并且无法访问PC,因为它位于远程位置。 我希望找到一种方法来使用AppDomain / UnhandledExceptionEventHandler来捕获所有未处理的exception并将其记录下来进行诊断。 但是,我(在故意)在类“DoSomething.class”中的办公室中创建的exception正在崩溃WinForm应用程序而不是记录exception错误。 我的代码是: //Current domain for the unhandled exception handling AppDomain currentDomain; [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)] public MainForm() { InitializeComponent(); currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler); CreateTimer.Interval = Settings.Default.CreateTimer; CreateTimer.Start(); RunCopyProtocol(); ChangeFilesTimer.Interval = 10000; ChangeFilesTimer.Start(); RunChangeFiles(); throw new Exception(“ABC”); } public void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) { […]

inheritanceException类以记录所有后续发生的exception

如果我想在整个应用程序中记录所有出现的exception, 我应该inheritanceException类并抛出该类的所有exception, 其构造函数将记录错误详细信息.. 或任何想法或建议???