Java异常帮助

发布于 2024-11-16 00:52:22 字数 176 浏览 0 评论 0原文

有人可以向我解释一下以下代码中的抛出异常部分吗?

public static void main(String args[]) throws Exception {

   //do something exciting...

}

先感谢您。

Can someone please explain to me the throws Exception part in the following code?

public static void main(String args[]) throws Exception {

   //do something exciting...

}

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ζ澈沫 2024-11-23 00:52:22

这意味着函数 main(String[]) 可以抛出 Exception 的任何子类型。在Java中,方法抛出的所有异常(除了RunTimeException)都必须显式声明

这意味着每个使用 方法 main(String[]) 都必须小心(try,catch) Exception,或者也将自身声明为抛出异常

it means the function main(String[]) can throw any sub-type of Exception. in Java, all exceptions thrown by a method(except RunTimeException) must be explicitly declared.

that means every method using main(String[]) will have to take care (try,catch) Exception, or declare itself as throwing Exception as well.

最终幸福 2024-11-23 00:52:22

异常是 Java 在发生意外情况时采取行动的一种方式。例如,如果您想从文件中读取/写入文件,则必须处理文件出现问题时将抛出的IOException

用一个小例子向您解释这一点:

让我们采用一个名为 method1() 的方法,该方法会引发异常:

public void method1() throws MyException {
  if (/* whatever you want */)
    throw new MyException();
}

它可以通过两种方式使用。第一种使用 method2() 的方法只会把烫手山芋进一步扔下去:

public void method2() throws MyException {
  method1();
}

第二种使用 method3() 的方法将处理该异常。

public void method3() {
  try {
    method1();
  }
  catch (MyException exception) {
  {
    /* Whatever you want. */
  }
}

有关异常的更多信息,http://download.oracle.com/javase/tutorial/ Essential/Exceptions/ 应该有帮助。


编辑

假设我们要返回此数组中某个值的包含内容(即输入数字的平方): int[] squares = {0, 1, 4, 9, 16, 25};0 如果数字(输入)太大。

行人编程:

if (input > squares.length)
  return 0;
else
  return squares[input];

例外大师编程:

try {
  return squares[input];
}
catch (ArrayIndexOutOfBoundException e) {
  return 0;
}

第二个示例更清晰,因为您还可以在此之后添加另一个块(然后再添加另一个块),以便解决每个可能的问题。例如,您可以在末尾添加以下内容:

catch (Exception e) { // Any other exception.
  System.err.println("Unknown error");
}

Exceptions are a way Java uses to act when something unexpected happened. For example, if you want to read/write from/to a file, you have to handle IOException that will be thrown if there is a problem with the file.

A little example to explain that to you:

Let's take a method called method1() that throws an exception:

public void method1() throws MyException {
  if (/* whatever you want */)
    throw new MyException();
}

It can be used in two ways. The first way with method2() will simply toss the hot potatoe further:

public void method2() throws MyException {
  method1();
}

The second way with method3() will take care of that exception.

public void method3() {
  try {
    method1();
  }
  catch (MyException exception) {
  {
    /* Whatever you want. */
  }
}

For more information about exceptions, http://download.oracle.com/javase/tutorial/essential/exceptions/ should help.


EDIT

Let's say we want to return the containt of a value in this array (which is the square of the entered number): int[] squares = {0, 1, 4, 9, 16, 25}; or 0 if the number (input) is too big.

Pedestrian Programming:

if (input > squares.length)
  return 0;
else
  return squares[input];

Exception Guru Programming:

try {
  return squares[input];
}
catch (ArrayIndexOutOfBoundException e) {
  return 0;
}

The second example is cleaner, for you can also add another block (and another again) after that, so that every possible problem is fixed. For example, you can add this at the end:

catch (Exception e) { // Any other exception.
  System.err.println("Unknown error");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文