捕捉/修改(消息)同类型/重新抛出异常 [英] Catch/Modify (Message)/Rethrow Exception of same type

查看:135
本文介绍了捕捉/修改(消息)同类型/重新抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个中心位置,以提取从异常信息,设定我需要它的消息参数,然后再次抛出该信息作为相同类型的一个异常

I want a central place to extract information from an exception, set all the information I need to its message parameter and then rethrow that information as an Exception of the same type.

更好的解决方案很可能是在那里的异常最终被处理(和它的消息记录)的地方要做到这一点,但..我有超过抛出异常的地方的控制,而不是在接收的地方例外,只记录其信息的内容。

The better solution probably would be to do this at the place where the exception is finally being handled (and its message logged), but.. I have control over the place throwing the exception, and not over the place that receives the exception and only logs its Message content.

除此之外的设计决定,并给予该消息是一个只读属性,我会(?)中创建一个新的异常对象?某种程度上,有没有一种方法,使新的异常对象相同的类型和原来的

Apart from that design decision and given that message is a readonly property, I would have (?) to create a new Exception object in some way, is there a way to make the new exception object the same type as the original one?

下面是我的代码,它不会编译 - 它绊倒了罚球线(在这里我将尝试动态投的对象)。

Here is my code, which does not compile - it stumbles over the throw line (where I try to dynamically cast the object).

public static void RethrowExceptionWithFullDetailInMessage(string msg, Exception ex)
{
    Exception curEx = ex;
    int cnt = 0;
    while (curEx != null)
    {
        msg += "\r\n";
        msg += cnt++ + " ex.message: " + curEx.Message + "\r\n";
        msg += "Stack: " + curEx.StackTrace;
        curEx = curEx.InnerException;
    }
    object newEx = Convert.ChangeType(new Exception(msg), ex.GetType());
    throw (ex.GetType())newEx;
}



这是否

Does this

throw (Exception)newEx;



保存类型? (它编译。)

preserve the type? (It compiles.)

是否Convert.ChangeType确保我得到正确类型的异常?

Does the Convert.ChangeType make sure I get an Exception of the correct type?

推荐答案

你正在尝试在这里做,因为它似乎有很多的陷阱要考虑的是不容易。

What you are trying to do here is not as easy as it seems and there are lots of pitfalls to consider.

记住,即Convert.ChangeType()将一种类型转换为另一种(假设这样的路径存在,就像一个字符串转换为一个int为例)。大多数异常不会做这(为什么他们会?)

Remember, that Convert.ChangeType() will convert one type to another (assuming such a path exists, like converting a string to an int for example). Most exceptions wont do this (Why would they?)

为了拉这一关,你就必须使用的GetType()方法来检查在运行时的异常类型和找到有你能满足并调用它要求一个构造函数。这里要小心,因为你没有在如何的所有的例外规定也不能保证,你将有机会获得标准的构造。

IN order to pull this off, you would have to examine the exception type at runtime with the GetType() method and locate a constructor that has requirements you can satisfy and invoke it. Be careful here, since you don't have control over how all exceptions are defined there is no guarantee you will have access to "standard" constructors.

话虽这么说,如果你觉得自己是一个规则破坏者,你可以做这样的事情...

That being said, if you feel like being a rule breaker you could do something like this...

void Main()
{
    try
    {   
        throw new Exception("Bar");
    }
    catch(Exception ex)
    {
        //I spit on the rules and change the message anyway
        ex.GetType().GetField("_message", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ex, "Foo");
        throw ex;
    }
}

这篇关于捕捉/修改(消息)同类型/重新抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆