Tag: exception处理

如何从嵌套的try-catch块中重新抛出先前的exception? (C#)

我有尝试类型转换的代码。 如果失败,我想尝试别的东西,如果失败,则重新抛出第一次转换尝试的原始exception。 问题是,我知道重新抛出的唯一方法就是“ throw; ‘坐在拦截区的尽头。 当我只希望从另一个catch块中发生重新抛出时会发生什么? try { valueFromData = Convert.ChangeType(valueFromData, pi.PropertyType); } catch(InvalidCastException e) { Debug.WriteLine(String.Concat(“Info – Direct conversion failed. Attempting to convert using String as an intermidiate type.”)); try { valueFromData = Convert.ChangeType(valueFromData.ToString(), pi.PropertyType); } catch { throw e; } } 如上所示,我必须使用’ throw e; ‘,它会重置调用堆栈。 到目前为止我唯一的解决方法是(imo)粗略: bool handled = true; … catch { […]

为什么手动引发的瞬态错误exception被处理为AggregateException?

当我尝试手动引发瞬态exception时,它总是作为AggregateException处理。 由于它作为AggregateException处理,因此在我的重试策略中不会将其作为暂时性error handling,也不会针对预定义的重试计数进行重试。 此处显示瞬态错误。 因此,我尝试了CommunicationException和ServerErrorException但它作为AggregateException处理。 当我查找AggregateException时,它会显示“表示在应用程序执行期间发生的一个或多个错误”。 是的,它非常有用!!! 以下是我案例的示例代码: 我有一个使用ServiceBusTransientErrorDetectionStrategy的重试策略 public void TestManually() { var retryPolicy = new RetryPolicy(RetryStrategy.DefaultFixed); retryPolicy.Retrying += (obj, eventArgs) => { Trace.TraceError(“Hey!! I’m Retrying, CurrentRetryCount = {0} , Exception = {1}”, eventArgs.CurrentRetryCount, eventArgs.LastException.Message); }; retryPolicy.ExecuteAsync(() => MyTestFunction().ContinueWith(t => { if (t.Exception != null) { // A non-transient exception occurred or retry limit […]

处理未处理的exception后恢复程序执行

问题:我通常使用AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException捕获未处理的exception。 现在的问题是,使用此exception处理程序 Public Sub OnUnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) Console.WriteLine(e.ExceptionObject.ToString()) Console.WriteLine(“Press Enter to continue”) Console.ReadLine() ‘Environment.Exit(1) End Sub 如果我不退出程序,程序执行仍会抛出exception。 现在我想知道如何将其更改为“on error resume next”行为? 有可能吗? _ Public Sub Main(ByVal argv As String()) ‘For i As Integer = 0 To argv.Length – 1 Step 1 ‘Console.WriteLine(“Argument {0}: {1}”, i, argv(i)) ‘Next […]

Restful service returns – 检查System.Byte 类型的对象的start元素时出错。 遇到意外的角色’ÿ’

所以我试图将图像从客户端发送到服务。 服务定义看起来像这样(实现没什么特别的,它没有到达那里,所以排除该代码): [OperationContract] [WebInvoke(Method = “POST”, UriTemplate = “Image”)] Stream Image(byte [] Image); 调用客户端看起来像这样: public static byte[] ImageToByte(Image img) { ImageConverter converter = new ImageConverter(); return (byte[])converter.ConvertTo(img, typeof(byte[])); } string uri = “http://localhost:8000/RestfulService/Image”; Bitmap img = new Bitmap(“Random.jpg”); byte[] byteArray = ImageToByte(img); var request = WebRequest.Create(uri) as HttpWebRequest; if (request != null) { request.ContentType = […]

打开文件时处理错误的最佳方法

处理文件(打开)是一种特别容易出错的活动。 如果您要编写一个函数来执行此操作(尽管是微不足道的),那么在处理错误的wrt中编写它的最佳方法是什么? 以下是好的吗? if (File.Exists(path)) { using (Streamwriter ….) { // write code } } else // throw error if exceptional else report to user 上述(虽然不是语法正确)是一个很好的方法吗?

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

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

每个appdomain的webapi和未处理的exception挂钩

拥有WebApi 2.2应用程序 – 是否可以使用AppDomain的UnhandledException钩子? 我有一个类似于这样的代码: [assembly: OwinStartup(“DevConfiguration”, typeof(DevStartup))] namespace WebApi.Module { public class DevStartup { private const string ErrorEventSourceName = “WebApiHost”; private const int ErrorEventId = 600; [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)] public void Configuration(IAppBuilder app) { AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException; … throw new Exception(“some exception); } public static void AppDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e) { EventLog.WriteEntry(ErrorEventSourceName, ex.ToString(),EventLogEntryType.Error, ErrorEventId); } […]

是否可以在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) { […]

全局捕获在后台线程中从WCF异步调用抛出的exception

我有一个与WCF服务通信的WPF应用程序。 我正在使用以下基于async的模式从我的ViewModel调用我的WCF服务(我正在使用MVVM模式): public async override void MyCommandImplementation() { using (var proxy = new MyProxy()) { var something = await proxy.GetSomethingAsync(); this.MyProperty = something; } } 当我遵循MVVM模式时,我有我的ViewModel公开的ICommand公共属性,因此关联的命令实现不会返回Task对象,因为它们就像事件处理程序一样。 所以exception处理实际上非常简单,即我能够使用以下模式捕获从我的WCF服务抛出的任何exception: public async override void MyCommandImplementation() { try { using (var proxy = new MyProxy()) { var something = await proxy.GetSomethingAsync(); } } catch (FaultException ex) { // Do […]

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

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