简体   繁体   English

需要更好地解释 Java 中的 try/catch/throw/throws 异常处理

[英]Need a better explanation for try/catch/throw/throws exception handing in Java

I am a little unsure about the difference between the try/catch, throw and throws in Java and every website seems to have an inhuman way of explaining it, I have tried the Oracle website but I just could not understand their explanation, is this correct?我有点不确定 Java 中的 try/catch、throw 和 throws 之间的区别,每个网站似乎都有一种非人道的解释方式,我尝试过 Oracle 网站,但我无法理解他们的解释,这是正确的?

Try/catch试着抓

With try catch, I want to try a piece of code and if something goes wrong do this.使用 try catch,我想尝试一段代码,如果出现问题,请执行此操作。

Throw

With throw, I am throwing an error because I want to?使用throw,我抛出一个错误是因为我想要? So if I wanted to validate a users age, say all people over 20 and the user does not meet the requirements, I would throw an error?所以如果我想验证一个用户的年龄,比如说所有 20 岁以上的人并且用户不符合要求,我会抛出错误吗?

Throws投掷

With Throws I am throwing an error but something else handles the exception?使用 Throws 我抛出一个错误但其他东西处理异常? Would this be another method/class?这是另一种方法/类吗?

Try / Catch试着抓

try
{
    // try to do some actions
}
catch(SomeExceptionType exception)
{
    // if the above actions throw a SomeExceptionType, do these actions
}

Throw

Correct.正确的。 We're explicitly throwing an exception.我们明确地抛出异常。 You may do this if, for example, the caller of a method has violated your method's contract.例如,如果某个方法的调用者违反了您的方法契约,您就可以这样做。 Perhaps an argument cannot be negative.也许一个论点不能是否定的。

In this situation, the best way to deal with this is to throw an exception which stops what we're doing and allows callers further up the stack to deal with the problem:在这种情况下,最好的处理方法是抛出一个异常,停止我们正在做的事情,并允许调用者进一步处理这个问题:

/** Do a thing. myInt must be positive */
void someMethod(Integer myInt)
{
    if (myInt < 0)
    {
        throw new IllegalArgumentException("Can't be negative");
    }
    // do something
}

void myCaller()
{
    someMethod( 1); // won't throw
    someMethod(-1); // will throw
}

Throws投掷

throws is used as a keyword when dealing with checked exceptions . throws在处理检查异常时用作关键字。 It's a way of letting callers know what checked exceptions they can expect they may have to deal with.这是一种让调用者知道他们可能需要处理哪些已检查异常的方法。

Those methods can decide to deal with the problem (ie a try-catch) or can themselves declare the same exception type to be thrown to propagate the exception up to their caller (and so on and so on)这些方法可以决定处理问题(即 try-catch),也可以自己声明要抛出的相同异常类型以将异常传播给调用者(等等)

You forget a important point : in Java all exceptions are not handled by the compiler in the same way.您忘记了一个重点:在 Java 中,编译器不会以相同的方式处理所有异常。

The compiler ensures only that the checked exceptions be handled.编译器确保处理已检查的异常。
These exceptions don't inherit from RuntimeException but from Exception (directly or indirectly).这些异常不是从RuntimeException继承的,而是从Exception (直接或间接)继承的。

So whatever you throw in your code or a method of a third class declares throwing a checked exception, you have to explicitly handle it.因此,无论您在代码中抛出什么或第三个类的方法声明抛出已检查异常,您都必须显式处理它。

And there you have two ways :你有两种方法:

  • catching the exception捕获异常

  • letting the exception be propagated to the caller让异常传播给调用者

try/catch addresses the first way while specifying the throws modifier in the method declaration addresses the second. try/catch解决第一种方法,而在方法声明中指定throws修饰符解决第二种方法。

For no RunTimeException s, you don't have this constraint as the compiler doesn't force you to handle it.对于没有RunTimeException s,您没有此约束,因为编译器不会强制您处理它。 You may handle them if you want to.如果你愿意,你可以处理它们。

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

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