简体   繁体   English

关于Java异常的初学者问题

[英]beginner question about exception in java

when an exception occurs that a method is unable to handle - does the program terminate and show the error number ? 当发生方法无法处理的异常时,程序是否终止并显示错误号? where does the error number and information about the error come from ? 错误号和错误信息从何而来? should the programmer while coding have an idea what kind of exception might occur. 程序员在编码时是否应该知道可能发生哪种异常。 if so why does'nt he ensure that exception does not occur . 如果是这样,他为什么不确保不会发生异常。

If you are using Java APIs, the exceptions that each method throws are documented. 如果使用Java API,则会记录每个方法引发的异常。

When the program terminate, it shows an stacktrace of the methods calls that caused that specific problem. 程序终止时,它会显示导致该特定问题的方法调用的堆栈跟踪。

Check the lesson on Exceptions from The Java Tutorial . 查看Java教程中有关异常的课程 You can learn much more reading there than reading my answer here :) 您可以在这里了解更多信息,而不是在这里阅读我的答案:)

There are 2 main types of exceptions in Java: Java中有两种主要的异常类型:

- checked
- unchecked

unchecked exceptions are further broken into RuntimeException and Error. 未检查的异常进一步细分为RuntimeException和Error。

RuntimeExceptions are programmer errors (ArrayIndexOutOfBoundsException) and Errors are things that are problems in the VM (OutOfMemoryError). RuntimeExceptions是程序员错误(ArrayIndexOutOfBoundsException),而Errors是VM中的问题(OutOfMemoryError)。

You should not catch RuntimeExceptions - you should fix your code so it does not throw the exception. 您不应该捕获RuntimeExceptions-您应该修复代码,以便它不会引发异常。

You should not catch Errors since the VM is likely in a state that you cannot do anything to recover from them. 您不应捕获错误,因为VM可能处于无法采取任何措施从错误中恢复的状态。

If your main does not catch an unchecked exception it will crash. 如果您的主体没有捕获到未经检查的异常,它将崩溃。

Java Exceptions bubble-up to the point where someone catches them, or to the point the program exits. Java异常会一直上升到有人抓住它们或程序退出的地步。 In the real-world, when many frameworks are used, exceptions never bubble-up to the top. 在现实世界中,当使用许多框架时,异常永远不会冒出来。 They are caught and reported (printed on console). 他们被捕获并报告(打印在控制台上)。 Catching exceptions is done by the try { } catch(..) { } block. 捕获异常是通过try { } catch(..) { }块完成的。

There are two types of exceptions - checked and unchecked. 异常有两种类型-选中和未选中。 The checked exceptions must be declared in the method signature (unlike the unchecked) 必须在方法签名中声明已检查的异常(与未检查的异常不同)

should the programmer while coding have an idea what kind of exception might occur 程序员在编码时是否应该知道可能发生哪种异常

Yes, but no one's perfect. 是的,但没有人是完美的。

why does'nt he ensure that exception does not occur 他为什么不确保不会发生异常

In exceptional circumstance, you WANT an exception. 在特殊情况下,您想例外。 You do not want to ignore the exception. 您不想忽略该异常。 For example, suppose that your program creates a file to save user settings. 例如,假设您的程序创建了一个文件来保存用户设置。 If for some reason the file creation fails (which your program has no control over, it's the Operating System's job), you do not want to go on like nothing happened. 如果由于某种原因文件创建失败(您的程序无法控制,这是操作系统的工作),则您不想继续进行,就像什么都没发生一样。 You want there to be an exception, so that whoever or whatever function called that function knows about this problem, and can do something, eg inform the user. 您希望有一个例外,以便任何人或任何调用该函数的函数都知道此问题,并且可以执行某些操作,例如通知用户。

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

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