简体   繁体   English

如何正确处理错误

[英]How to handle errors properly

I'm a beginner in java writing an frontend for a webservice.I have to validate the input to get useful error messages for the user.Currently it works this way: 我是用Java编写Web服务前端的初学者,我必须验证输入以获取对用户有用的错误消息,目前它以这种方式工作:

public Object zipVal(String zip)
{
    try
    {
        if (zip.length() == 5)
        {
            val.setZip(Integer.parseInt(zip));
            return val.getZip();
        } else
        {
            return lengthError;
        }

    } catch (NumberFormatException e)
    {
        return formatError;
    }
}

for zip Codes.Using Objects to declare the return type is not what I want tho(and is afaik discouraged),but I'm not sure how I should handle wrong inputs other than that.Should I just return null for every Exception and invalid input and handle such things in another method? 邮编。使用对象声明返回类型不是我想要的(不鼓励使用afaik),但是我不确定应该如何处理错误的输入,我应该为每个Exception和invalid返回仅null用另一种方法输入和处理这样的事情?

Edit:Added something to actually throw an Exception... 编辑:添加了一些东西以实际引发异常...

Yeah, exception handling might be one of the trickier things to consider (if one comes from a C programming background for example, where we used to be happy with < 0 return code for indicating erroneous program flow). 是的,异常处理可能是要考虑的棘手问题之一(例如,如果来自C编程背景,我们曾经对<0表示错误程序流的返回代码感到满意)。 Normally you are pretty safe off by catching other API:s you integrate with and encapsulate them in your own exception (sort of masking them away), but by doing so don't forget to chain the original exception into your own with this constructor (and/or derivatives of such): 通常,您可以通过捕获其他API来确保自己的安全:将其他API集成并封装在您自己的异常中(将它们掩盖起来),但是这样做不要忘记使用此构造函数将原始异常链接到您自己的异常中(和/或此类衍生物):

public MyException(final String message, final Throwable cause) {
        super(message, cause);
}

One surely see alot of: 当然可以看到很多:

catch (Exception) {
    return null;
}

and such in code as well, I wouldn't say that this is "good" object orientation, but it is still common and could be used in special occasions. 而且在代码中也是如此,我不会说这是“好的”面向对象,但是它仍然很常见,可以在特殊场合使用。

And also, its usually very important what you do (how to handle) when you catch the exception, someone told me that programing is 90% about error control and 10% about functionality :) 而且,通常,在捕获异常时您要做什么(如何处理)非常重要,有人告诉我,编程中90%涉及错误控制,而10%涉及功能:)

Here are some tutorials/resources: 以下是一些教程/资源:
http://docs.oracle.com/javase/tutorial/essential/exceptions/ http://howtodoinjava.com/2013/04/04/java-exception-handling-best-practices/ http://docs.oracle.com/javase/tutorial/essential/exceptions/ http://howtodoinjava.com/2013/04/04/java-exception-handling-best-practices/

If you are returning a value, then there is no need to handle the exception. 如果要返回值,则无需处理该异常。 It is better you declare that the method may throw the exception. 最好声明该方法可能引发异常。

NumberFormatException is a RunTimeException. NumberFormatException是RunTimeException。 So if you wish to handle it, then better return an invalid zip (say -1) to let the caller know that something went wrong. 因此,如果您希望处理它,则最好返回一个无效的zip(例如-1),以使呼叫者知道出了点问题。

Otherwise, declare that you will throw a Custom Exception if NFE occurs. 否则,声明如果发生NFE,则将引发Custom Exception。

This snippet may be useful. 此代码段可能有用。

public int setZipVal(String zip) // throws CustomException
{
    try
    {
        if (zip.length() == 5)
        {
            val.setZip(Integer.parseInt(zip));
            return val.getZip();
        } 

    } catch (NumberFormatException e)
    {
       // Log the error and return invalid zip
       return -1;

       // OR throw custom exception
       throw new CustomException("Length Error"));

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM