处理和未处理的异常有什么区别? [英] What is the difference between handled and un-handled exceptions

查看:200
本文介绍了处理和未处理的异常有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Java中处理的和未处理的异常之间的区别。
哪一个我应该使用一个try / catch块,当我必须扩展throwables。



还有,如果我扩展可抛弃,我总是要处理异常在一个单独的类?



如果异常要在一个单独的类中处理,我可以创建自定义功能(例如,调用另一种方法或中断循环),而不是仅仅使用超级构造函数自定义消息?

解决方案

我假设处理和未处理的异常,意思是检查和取消选中。 b
$ b

未检查的异常 - 扩展RuntimeException的所有类都称为未选中。他们通常会指示编程错误,例如逻辑错误或
不当使用API​​。



未检查异常的示例:

 ●ArithmeticException 
●NullPointerException
●IndexOutOfBoundsException
●IllegalArgumentException
●ClassCastException

例如,如果您尝试访问仅有5个单元格的数组的第10个单元格,则会导致ArrayIndexOutOfBoundsException。这是一个程序员的错误和程序员的错误,所以应该这样对待。该异常不应该使用try / catch来处理,而应该使用if语句检查数组的大小。



检查的异常 s - 这是一个非常好的条件,一个写得很好的应用程序应该预期和恢复。 Java中检查的异常扩展了Exception类,但不扩展RuntimeException类。检查的异常受 捕获或指定要求 的限制:



代码可能会引发检查异常,该方法必须处理此异常或指定它可能会引发此异常



已检查异常的示例:

 ●FileNotFoundException 
●IOException
●SQLException

例如,您可能有一个非常好的代码,将数据读取或写入文件,但该文件可能会被其他用户从文件系统中突然删除。这不是程序员的错误,但是可以发生,所以您必须必须预期并处理这种情况。



所以总结一下:
未被检查的异常不应该使用 try / catch 处理 - 它们应该被视为一个错误,应该被固定(或者避免使用如果语句)。当然,您可以使用try / catch块来处理一个 RuntimeException (例如 NullPointerException ),但它是

检查异常必须使用 try / catch 块或方法不知道如何处理它们时,应该声明它们被方法本身抛出。因此,处理异常的责任将转移到将调用此方法的方法。这就是抓住或指定的要求。


I'd like to know the difference between handled and unhandeled Exceptions in Java. which one i should use a try/catch block and when I have to extend throwables.

also, if i extend throwables, do i always have to handle the exception in a separate class?

If exception is to be handled in a separate class, can i create custom functionality (e.g. Invoke another method or break a loop) instead of overriding the super constructor with just a custom message?

解决方案

I presume by 'handled and unhandled' exceptions you mean 'checked and unchecked'.

Unchecked exceptions - All classes which extend RuntimeException are called unchecked. They usually indicate programming bugs, such as logic errors or improper use of an API.

Example of unchecked exceptions:

● ArithmeticException
● NullPointerException
● IndexOutOfBoundsException
● IllegalArgumentException
● ClassCastException

For example if you try to access the 10th cell of an array with 5 cells only, it would cause an ArrayIndexOutOfBoundsException. This is a programmer bug and a programmer`s fault, so it should be treated as such. This exception should not be handled with try/catch, but instead should be checked with an if statement for the size of the array.

Checked exceptions - These are exceptional conditions that a well-written application should anticipate and recover from. Checked exceptions in Java extend the Exception class but do not extend RuntimeException class. Checked exceptions are subject to the "Catch or Specify Requirement":

When in method's body some code may throws checked exception, the method must either handle this exception or specify that it may throws this exception

Examples of checked exceptions:

● FileNotFoundException
● IOException
● SQLException

For example you may have a perfectly well-written code that reads or writes data into a file, but the file may be suddenly deleted from the file system by another user. This is not a programmer`s error, but it CAN happen, so you MUST anticipate and handle this situation.

So to summarize: Unchecked exceptions SHOULD NOT BE handled with try/catch - they should be treated as a bug and should be fixed (or avoided with if statements). Ofcourse you can use a try/catch block to handle a RuntimeException (for example NullPointerException), but it is NOT a good practice.

Checked exceptions MUST BE either handled with try/catch blocks or when the method does not know how to handle them, they should be declared to be thrown by the method itself. Thus the responsibility for handling the exception is transferred to the methods that would invoke this very method. This is what the Catch or Specify requirement says.

这篇关于处理和未处理的异常有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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