Tag: 例外

定制exception处理程序

我需要编写自定义exception处理程序… 我的应用程序可能会抛出不同类型的exception,我希望它们都由一个处理程序处理(这样我就不需要使用数千个“try-catch”块)。 我尝试使用AppDomain的UnhandledException处理程序,但似乎在捕获exception时,应用程序将不可避免地被关闭。 那么,有什么方法可以实现我的想法吗? 编辑: 感谢您的快速回复,但我想让您更好地理解我的情况:例如,我有一个抛出自定义exception的类(可逆)。 现在,这个类被多个其他类调用,所以我需要在它们的每一个上写一个try-catch块(或者至少这是你的回复让我想到的).. 所以,需要有一个能够捕获它们的处理程序……

有没有办法在WinForms .NET 3.5应用程序中为未处理的exception定义操作?

请注意,我意识到这已在此处解决。 那篇文章讨论了.NET 1.1中的exception处理,同时暗示有一个更好的解决方案> .NET 2.0所以这个问题具体是关于更新的.NET版本。 我有一个Windows窗体应用程序,预计会频繁和意外地失去与数据库的连接,在这种情况下,它将自身重置为其初始状态。 我已经通过我的自定义DBWrapper对象上的一组装饰器进行错误记录,重试连接等。 然而,在那之后,我想让错误通过堆栈。 一旦它到达顶部并且未处理,我希望它被吞下并且我的ApplicationResetter.Reset()方法被执行。 谁能告诉我怎么做? 如果这是不可能的,那么至少有一种方法可以处理这个问题,而不会将ApplicationResetter依赖于每个可能收到此类错误并且实际上没有关闭并重新启动我的应用程序的类(这看起来很丑陋)?

在C#中重复一个函数,直到它不再抛出exception

我有一个调用SOAP接口的类,并获取一个数据数组。 但是,如果此请求超时,则会引发exception。 这很好。 但是,我希望我的程序再次尝试进行此调用。 如果它超时,我希望它继续拨打这个电话,直到它成功为止。 我怎么能做到这一点? 例如: try { salesOrdersArray = MagServ.salesOrderList(sessID, filter); } catch { ?? What Goes Here to FORCE the above line of code to rerun until it succeeds. }

为什么n.GetHashCode()工作,但n.GetType()抛出exception?

我在教自己C#(我还不太了解)。 在这个简单的例子中: bool? n = null; Console.WriteLine(“n = {0}”, n); Console.WriteLine(“n.ToString() = {0}”, n.ToString()); Console.WriteLine(“n.GetHashCode() = {0}”, n.GetHashCode()); // this next statement causes a run time exception Console.WriteLine(“n.GetType() = {0}”, n.GetType()); 直观地,我理解为什么GetType()方法会抛出exception。 实例n是null,这可以解释这一点,但是,为什么在使用n.GetHashCode()和ToString()时出于同样的原因我不会得到exception? 谢谢您的帮助, 约翰。

我的自定义exception是否应该inheritance与它们类似的exception,或者只是从exceptioninheritance?

我在我的应用程序中创建了一些自定义exception。 如果我有一个在测试参数状态后抛出的exception,或者我有一个在测试int在适当范围内后抛出的exception,我的exception是否会inheritanceArgumentException和IndexOutOfRangeException,还是应该inheritanceException?

无法确定呼叫者的应用程序身份?

我正在VS2010 for Windows Phone中编写一个Silverlight透视应用程序。 我刚刚在这里添加了msdn的示例代码。 现在,每次重新加载设计器时,我都会遇到exception: 无法确定调用者的应用程序标识。 在System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope范围,输入appEvidenceType) 在System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope范围,输入applicationEvidenceType) 在System.IO.IsolatedStorage.IsolatedStorageSettings.get_ApplicationSettings()位于C:.. \ Settings.cs中的SettingsSample.AppSettings..ctor():第34行 这是Visual Studio / Windows Phone SDK中的错误吗? 这是第34行构造函数中的代码: public AppSettings() { // Get the settings for this application. try { settings = IsolatedStorageSettings.ApplicationSettings; } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } 我添加了try-catch以查看发生了什么。 我怀疑Visual Studio(调用者)正在尝试运行代码,但没有关联的应用程序(应用程序标识),因此它失败了。 也许? 有什么想法吗?

解决问题“无法访问被处置对象。”例外

在我当前的项目中有一个Form类,如下所示: public partial class FormMain : Form { System.Timers.Timer timer; Point previousLocation; double distance; public FormMain() { InitializeComponent(); distance = 0; timer = new System.Timers.Timer(50); timer.AutoReset = true; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.Start(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (previousLocation != null) { // some code UpdateDistanceLabel(distance); UpdateSpeedLabel(v); } previousLocation = Cursor.Position; […]

C#忽略exception的最佳方法

可能重复: 忽略C#中的exception 有时在极少数情况下你真的想忽略exception。 做这个的最好方式是什么? 我的方法是捕获exception,但不做任何事情。 其他? try { blah } catch (Exception e) { }

如何使用FileInfo类,避免PathTooLongException?

我该如何使用(避免PathTooLongException): System.IO.FileInfo 路径大于260个字符? 是否有类似的类/方法返回相同的FileInfo类结果?

C#System.InvalidCastException

为什么我收到此错误? System.InvalidCastException was unhandled by user code Message=Specified cast is not valid. Source=System.Windows.Forms StackTrace: at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation() at System.Windows.Forms.WebBrowser.get_Document() at System.Windows.Forms.WebBrowser.get_DocumentStream() at System.Windows.Forms.WebBrowser.get_DocumentText() at SiteBot.MainWindow.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:\Documents\Visual Studio 2010\Projects\SiteBot\MainWindow.cs:line 35 at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument) InnerException: