English 中文(简体)
C# Catch re-throw/propagation for a specific special category
原标题:C# Catch re-throw/propagation for a specific exception type

我正试图向我宣传你能够看到的关卡内松森采信。 我希望,除这一例外情况外,这部法典能够涵盖所有事项,但我希望介绍层能够赶上向用户通报,数据库是问题,因此他可以去并固定。 我的问题是,当我第一次扔下它时,我就拿到关卡内松森采行器,而没有在网关特收割条款上由用户代码处理。

还必须注意到延伸例外的关卡内松森广度。 我是否失踪了,或者我是否必须把所有捕获量移至列报层?

        try
        {
             something();
        }
        catch (GatewayConnectionFailedException gcfe)
        {
            throw;
        }
        catch (GatewayException ge)
        {
            if (ge.GetType() == typeof(GatewayConnectionFailedException))
                throw;
            string errMsg = "Records could not be retrieved due to a data gateway error. " + GetTypeInfo();
            _logger.Error(errMsg + "
{0}", ge);
        }
        catch (Exception e)
        {
            if (e.GetType() == typeof(GatewayConnectionFailedException))
                throw;
            string errMsg = "Records could not be retrieved due to an unexpected error. " + GetTypeInfo();
            _logger.Error(errMsg + "
{0}", e);
        }
最佳回答

Stupid question... is your UI code try-catching in it s call to this layer? Something has to handle that second throw...

简言之,它像你再次试图这样做一样:

using System;

namespace ConsoleApplication1
{
    class ExceptionA : Exception
    {
        public override string Message
        {
            get
            {
                return "Exception A";
            }
        }
    }

    class ExceptionB : ExceptionA
    {
        public override string Message
        {
            get
            {
                return "Exception B";
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                DoThing();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught in  UI  code: " + ex.Message);
            }
        }

        static void DoThing()
        {
            try
            {
                throw new ExceptionB();
            }
            catch (ExceptionB ex)
            {
                Console.WriteLine("B号");
                throw;
            }
            catch (ExceptionA ex)
            {
                Console.WriteLine("Caught A");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught Generic");
            }
        }
    }
}

产生这一产出:

B号
Caught in UI code: Exception B
Press any key to continue...

似乎像你一样,没有达到第2次被推翻的例外情况,这就是为什么它“无所作为”。 如果我们主要评论试捕副渔获物,我们最后有一个未经处理的例外情况:

static void Main(string[] args)
        {
            //try
            //{
            DoThing();
            //}
            //catch (Exception ex)
            //{
            //Console.WriteLine("Caught in  UI  code: " + ex.Message);
            //}
        }

放弃以下产出:

B号

Unhandled Exception: ConsoleApplication1.ExceptionB: Exception B at ConsoleApplication1.Program.DoThing() in C:UsersGiovanniAppDataLocalT emporary ProjectsConsoleApplication1Program.cs:line 50 at ConsoleApplication1.Program.Main(String[] args) in C:UsersGiovanniAppDa taLocalTemporary ProjectsConsoleApplication1Program.cs:line 33 Press any key to continue . . .

问题回答

如果不确定例外,如果你在追捕和重新种植该例外,就使用该守则:

catch (GatewayConnectionFailedException)
{
    throw;
}

这样一来,轮班就能够更准确地反映程程。 这个问题可能无法解决。

如果不全面了解情况,就很难说什么是缺失的,但你应该以不同方式提出例外。 The syntax should be

投掷;

页: 1 更多信息。

缩略语 应在<条码>上解决您的问题。 见/code>,don t , 抛下了标的。 回答是正确的。

第二,Im 假设GatewayConnectionFailedException 继承GatewayException<>。

选择<条码>副渔获物/条码> 继承顺序的顺序顺序,子女类别应当先到,然后是基础类别。

catch(Child){}
catch(Base){}
catch(Exception) {} //Base class for all exceptions

to start your try catch is redundant. the first catch will handle GatewayConnectionFailedException the remaining catches will never be of type GatewayConnectionFailedException because they were handled by the first catch. so the code can be simplified to

try
{
    something();
}
catch (GatewayConnectionFailedException)
{
    throw;
}
catch (GatewayException e)
{
    _logger.Error(e.Message, e);
}

现在,律师事务所将如何处理这一问题取决于你如何处理这一例外情况。 如果你只是放弃这一例外,那么你也需要在陈述层进行尝试。 然而,如果这一层的回落类型归还了类似物体

class Result<T>
{
     T Value {get;set;}
     Exception Error {get;set;}
}

那么,你就只能处理这种类型的问题,无需在专题介绍层进行尝试/捕获。

同样值得注意的是,你正在追捕什么,以及你为什么试图追捕。 通常,除应用层外,你不会发现错误和失败。 例外应当是例外的,因此,只能满足你预期会出现的例外情形,以及发生这种情况的原因。 否则,他们就会ub。

Instead of using throw exceptionName try only throw.

Edit 1:
Try catching all exceptions in the same block, then throw back the exception only if it s the GatewayConnectionFailedException

try
{
     something();
}
catch (Exception e)
{
    if (e.GetType() == typeof(GatewayConnectionFailedException))
        throw;
    string errMsg = "Records could not be retrieved due to an unexpected error. " + GetTypeInfo();
    _logger.Error(errMsg + "
{0}", e);
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...