“投掷"是什么意思这样做有什么帮助? [英] What does "Throws" do and how is it helpful?

查看:18
本文介绍了“投掷"是什么意思这样做有什么帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,刚刚遇到一个在方法中使用Thrrows"关键字的教程.我对此做了一些研究,但仍然没有真正理解它.

I am new to Java and have just came across a tutorial which uses the"Throws" keyword in a method. I have done a little research into this but still do not really understand it.

据我目前所见,它告诉编译器在该特定方法中可能会抛出某个异常.为什么我们需要告诉编译器这个?我已经在我的方法中仅使用 try-catch 语句制作了许多程序并且它运行得很好 - 肯定是这些 try-catch 语句管理异常,对吗?

From what I have seen so far, it is telling the compiler that a certain exception may be thrown in that particular method. Why do we need to tell the compiler this? I have made many programs using merely a try-catch statement in my methods and it has worked just fine - surely it is these try-catch statements which manage exceptions, right?

推荐答案

您可以使用 trycatch 管理方法的异常正如你所说.在这种情况下,您不需要使用 throws.例如:

You can manage an exception within a method using try and catch as you say. In that case, you do not need to use throws. For example:

public void myMethod()
{
  try {
    /* Code that might throw an exception */
  }
  catch (SpaceInvadersException exception) {
    /* Complicated error handling code */
  }
}

但是假设您有一千个方法,所有这些方法都可能抛出 SpaceInvadersException.然后,您最终将不得不编写所有复杂的错误处理代码一千次.当然,你总是可以创建一个带有 dealWithSpaceInvadersException() 方法的 ErrorHandler 类,你可以从它们调用,但你仍然会被一千个 卡住try-catch 块.

But suppose you had a thousand methods, all of which might throw a SpaceInvadersException. Then you would end up having to write all the complicated error handling code a thousand times. Of course, you could always create an ErrorHandler class with a dealWithSpaceInvadersException() method that you could call from them, but you'd still be stuck with a thousand try-catch blocks.

相反,您告诉编译器这数千个方法中的每一个都可能抛出 SpaceInvadersException.然后任何调用这些方法之一的方法都需要处理错误本身,要么使用 try-catch,要么告诉编译器 it 可能会抛出 SpaceInvadersException.这是使用 throws 关键字完成的,如下所示:

Instead, you tell the compiler that each of these thousand methods could throw a SpaceInvadersException. Then any method that calls one of these methods needs to deal with the error itself, either by using a try-catch, or by telling the compiler that it might throw a SpaceInvadersException. This is done using the throws keyword, like this:

public void myMethod() throws SpaceInvadersException
{
  /* Code that might throw an exception */
}

public void callingMethod()
{
  try {
    myMethod();
  }
  catch (SpaceInvadersException exception) {
    /* Complicated error handling code */
  }
}

在这种情况下,您需要通知编译器 myMethod 可能会抛出 SpaceInvadersException.这意味着您不能在不以某种方式处理异常的情况下调用该方法(try-catch 或使用 throws 关键字在调用方法).如果 throws 不存在,您可以在不进行任何异常处理的情况下调用该方法,并获得程序中任何地方都没有处理的异常(这会很糟糕).

In that case, you need to inform the compiler that myMethod could throw a SpaceInvadersException. This means that you can't call that method without dealing with the exception in some way (try-catch or using the throws keyword on the calling method). If throws weren't there, you could call the method without doing any exception handling, and get an exception that wasn't dealt with anywhere in the program (which would be bad).

因为避免代码重复总是更好,所以通常最好在比它高得多的函数中将错误处理交给 try-catch是在所有低级方法中单独处理.这就是这种机制存在的原因.

Since it is always better to avoid code duplication, it is normally better to palm off your error handling to a try-catch in a much higher level function than it is to deal with it separately in all of the low level methods. That is why this mechanism exists.

这篇关于“投掷"是什么意思这样做有什么帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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