没有异常返回错误的详细信息 [英] Return details of error without Exceptions

查看:86
本文介绍了没有异常返回错误的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是问:使用异常是不好的结束功能?



现在我明白我们不应该返回异常。那么,返回错误的正确方法是什么?



当我返回一个异常时,当某些事情失败时,我知道函数内的错误是什么。

但是如果是一个带有多个异常的void函数,我有一个布尔函数如果可以,则返回true,如果某些失败,则返回false,如何知道失败的确切事情? / p>

由于多种原因,一个函数可能失败,所以只有一个False才能保持我的程序不行。当我用一些文本返回异常时,我可以向用户显示确切的错误。



有一个字符串函数返回一个像正确或不同错误的单词看起来对我来说最好的。
什么是推荐的方法?

解决方案


...我怎么知道确切的事情失败了?


提出这个问题可能意味着你在方法中有太多的进展,现在是时候



以下是您以前的问题复制的示例:

  void Function1()
{
//做某事...
//如果有事情失败
抛出新异常(因为bla bla而失败);

//做某事...
//如果有事情失败
抛出新异常(因为bla bla bla bla而失败);

//如果一切都可以,只要继续,不要抛出任何东西
}


$ b $功能1正在做很多。为了使维护(和测试)更容易,您可以将单独的逻辑提取到单独的方法中,每个方法返回一个 bool 值以表示成功。

  bool SomethingOne()
{
var successful = true;

//做某事...
//如果某些失败
successful = false;

返回成功;
}

bool SomethingTwo()
{
var successful = true;

//做某事...
//如果某些失败
successful = false;

返回成功;
}

void Function1()
{
var isOneSuccessful = SomethingOne();
if(!isOneSuccessful)
{
// SomethingOne failed - do something
return;
}

var isTwoSuccessful = SomethingTwo();
if(!isTwoSuccessful)
{
// SomethingTwo failed - do something
return;
}

//如果一切都可以,只要继续,不要抛出任何东西
}

YMMV。我假设失败是一个非关键的问题。也许这是一个验证例程,你只是要显示一条消息,说请检查所有字段。



如果是程序员使用你的方法需要的东西要特别注意和处理,或者如果用户需要了解更多信息,一个简单的布尔值可能不会被删除。


I Just ask this: Is it bad to use exceptions to end function?.

Now I understand we should not return exceptions. So, what is the right way to return errors?

When I return an exception, when something fails, I know what was the error inside the function.

But if instead a void function with multiple exceptions, I have a boolean function returing true if it was OK, and false if something fails, how can I know the exact thing that fails?

A function can fails for multiple reasons, so returing just a False is not ok for my program. When I return an Exception with some text I can show to the user the exact error.

Having an string function an returning a word like "Correct" or the different errors doesn't look like the best thing for me. What is the recommended approach?

解决方案

"...how can I know the exact thing that fails?"

Asking that question might mean you've got too much going on in the method and it's time to refactor.

Here's your example, copied from your previous question:

void Function1()
{
    //Do something...
    //If something fail
    Throw New Exception("Fails because bla bla");

    //Do something...
    //If something fail
    Throw New Exception("Fails because bla bla bla bla");

    //If everything its ok, just continue without throwing anything
}

Function1 is doing a lot. To make maintenance (and testing) easier, you could extract groups of logic into separate methods, each of which returns a bool value to indicate success.

bool SomethingOne()
{
    var successful = true;

    //Do something...
    //If something fail
        successful = false;

    return successful;
}

bool SomethingTwo()
{
    var successful = true;

    //Do something...
    //If something fail
        successful = false;

    return successful;
}

void Function1()
{
    var isOneSuccessful = SomethingOne();
    if (!isOneSuccessful)
    {
        // SomethingOne failed - do something
        return;
    }

    var isTwoSuccessful = SomethingTwo();
    if (!isTwoSuccessful)
    {
        // SomethingTwo failed - do something
        return;
    }

    //If everything its ok, just continue without throwing anything
}

YMMV. I'm assuming that "something fails" is a non-critical problem. Like maybe this is a validation routine and you're just going to display a message that says "Please check all fields."

If it's something the programmer using your method needs to be aware of and handle specially, or if it's something the user has to know more about, a simple boolean may not cut it.

这篇关于没有异常返回错误的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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