“投掷”是什么做和有帮助吗 [英] What does "Throws" do and how is it helpful?

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

问题描述

我是Java的新手,刚刚遇到了一个在方法中使用Throws关键字的教程。我已经做了一些研究,但仍然不明白。



从目前为止我看到的,它告诉编译器一定的例外可能是扔在那个特定的方法。为什么我们需要告诉编译器呢?我已经在我的方法中使用了一些仅仅是一个try-catch语句的程序,并且它的工作很正常 - 这些try-catch语句管理异常,对吧?

解决方案

您可以使用 try catch / code>如你所说。在这种情况下,您不需要使用 throws 。例如:

  public void myMethod()
{
try {
/ *代码这可能会引发异常* /
}
catch(SpaceInvadersException异常){
/ *复杂的错误处理代码* /
}
}

但是假设你有一千种方法,所有这些方法都可能会抛出一个 SpaceInvadersException 。那么你最终必须编写所有复杂的错误处理代码一千次。当然,您可以随时创建一个 ErrorHandler 类,其中包含您可以从他们那里调用的 dealWithSpaceInvadersException()方法您仍然会遇到一千个尝试 - catch 块。



而是告诉编译器,这千个方法中的每一个都可以抛出一个 SpaceInvadersException 。那么调用这些方法之一的任何方法都需要通过使用 try - catch 来处理错误本身,或者通过告诉编译器,它可能会抛出一个 SpaceInvadersException 。这是使用 throws 关键字完成的,如下所示:

  public void myMethod()抛出SpaceInvadersException 
{
/ *可能引发异常的代码* /
}

public void callMethod()
{
尝试{
myMethod();
}
catch(SpaceInvadersException异常){
/ *复杂的错误处理代码* /
}
}
pre>

在这种情况下,您需要通知编译器, myMethod 可以抛出一个 SpaceInvadersException 。这意味着你不能以某种方式处理异常( try - catch 或使用调用方法中的 throws 关键字)。如果 throws 不在那里,你可以调用该方法而不进行任何异常处理,并获得程序中的任何地方没有处理的异常(这将是坏的) )。



由于总是更好地避免代码重复,通常最好将错误处理掌握到 try - catch 在比所有低级别方法单独处理它更高级别的功能。这就是为什么这种机制存在。


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.

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?

解决方案

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 */
  }
}

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.

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 */
  }
}

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).

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天全站免登陆