" TryParse / Parse like"模式:实现它的最佳方法是什么 [英] "TryParse / Parse like" pattern: what is the best way to implement it

查看:82
本文介绍了" TryParse / Parse like"模式:实现它的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是来自如何指示方法不成功。 xxx()Tryxxx()模式在许多库中都非常有用。我想知道在不重复执行代码的情况下提供两种实现的最佳方法是什么。



什么是最好的:

  public int DoSomething(字符串a)
{
//可能抛出异常
}
public bool TrySomething(字符串a,输出结果)
{
尝试
{
结果= DoSomething(a)
返回true;
}
捕获(异常)
{
return false;
}

  public int DoSomething(string a)
{
int result;
if(TrySomething(a,out result))
{
返回结果;
}
其他
{
throw Exception(); //哪个例外?
}
}
public bool TrySomething(字符串a,输出结果)
{
// ...
}

我本能地认为第一个示例更正确(您确切地知道发生了哪个异常),但是try /赶上太贵了吗?在第二个示例中,是否有办法捕获异常?

解决方案

让TrySomething只是捕获并吞没异常是非常糟糕的理念。 TryXXX模式的一半是避免对性能的影响。



如果您不需要太多有关异常的信息,则可以使用DoSomething方法调用TrySomething并在失败时引发异常。如果您需要例外中的详细信息,则可能需要更详细的说明。我尚未确定异常对性能的影响最大的时间-如果是引发异常而不是创建异常,则可以编写一个私有方法,该方法的签名与TrySomething类似,但返回的异常或为null:

  public int DoSomething(字符串输入)
{
int ret;
异常异常= DoSomethingImpl(input,out ret);
if(exception!= null)
{
// //注意,这里的
抛出异常会丢失堆栈跟踪的准确性;
}
回返;
}

public bool TrySomething(字符串输入,输出int ret)
{
Exception exception = DoSomethingImpl(input,out ret);
返回异常== null;
}

私有异常DoSomethingImpl(字符串输入,输出为int ret)
{
ret = 0;
if(输入!=坏)
{
ret = 5;
返回null;
}
else
{
return new ArgumentException( Some details);
}
}

在确定时间之前,先尝试一下!

This question is a follow-up from How to indicate that a method was unsuccessful. The xxx() Tryxxx() pattern is something that can be very useful in many libraries. I am wondering what is the best way to offer both implementations without duplicating my code.

What is best:

public int DoSomething(string a)
{
     // might throw an exception
}
public bool TrySomething(string a, out result)
{
    try
    {
        result = DoSomething(a)
        return true;
    }
    catch (Exception)
    {
        return false;
    }

or

public int DoSomething(string a)
{
     int result;
     if (TrySomething(a, out result))
     {
         return result;
     }
     else
     {
         throw Exception(); // which exception?
     }
}
public bool TrySomething(string a, out result)
{
    //...
}

I'd instinctively assume that the first example is more correct (you know exactly which exception happened), but couldn't the try/catch be too expensive? Is there a way to catch the exception in the second example?

解决方案

Making TrySomething just catch and swallow the exception is a really bad idea. Half the point of the TryXXX pattern is to avoid the performance hit of exceptions.

If you don't need much information in the exception, you could make the DoSomething method just call TrySomething and throw an exception if it fails. If you need details in the exception, you may need something more elaborate. I haven't timed where the bulk of the performance hit of exceptions is - if it's the throwing rather than the creating, you could write a private method which had a similar signature to TrySomething, but which returned an exception or null:

public int DoSomething(string input)
{
    int ret;
    Exception exception = DoSomethingImpl(input, out ret);
    if (exception != null)
    {
        // Note that you'll lose stack trace accuracy here
        throw exception;
    }
    return ret;
}

public bool TrySomething(string input, out int ret)
{
    Exception exception = DoSomethingImpl(input, out ret);
    return exception == null;
}

private Exception DoSomethingImpl(string input, out int ret)
{
    ret = 0;
    if (input != "bad")
    {
        ret = 5;
        return null;
    }
    else
    {
        return new ArgumentException("Some details");
    }
}

Time this before you commit to it though!

这篇关于" TryParse / Parse like"模式:实现它的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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