checked exception 与 runtime exception

Java存在三种可抛出结构(throwable):受检异常(checked exception)、运行时异常(runtime exception)和错误(error)。使用原则是:

1、如果期望调用者能够恢复,则应该使用受检异常。抛出受检异常,可以强迫调用者在一个catch子句中处理该异常,或者继续向外传播。

2、运行时异常是指难以恢复或者不可恢复的程序错误。大多数运行时异常都是表明前提违例(precondition violation指API的调用方没有遵循调用约定)。

3、错误(error)被JVM保留用于指示资源不足、约束失败或者其他程序无法继续执行的情况。最好不要实现新的Error子类,所有抛出的未受检异常都应该是RuntimeException的子类。

运行时异常 可以依靠程序员编程的规范性来避免。

应用场景: 如果想终止程序 则抛出运行时异常  如果对异常想进行处理  则抛出检查异常。

checked exception 与 runtime exception,布布扣,bubuko.com

时间: 2024-08-13 08:57:11

checked exception 与 runtime exception的相关文章

[Android_Exception]Checked 和 Runtime Exception区别

Java里有个很重要的特色是Exception ,也就是说允许程序产生例外状况.而在学Java 的时候,我们也只知道Exception 的写法,却未必真能了解不同种类的Exception 的区别. 首先,您应该知道的是Java 提供了两种Exception 的模式,一种是执行的时候所产生的Exception (Runtime Exception),另外一种则是受控制的Exception (Checked Exception). 所有的Checked Exception 均从java.lang.E

Java中的checked exception和unchecked exception

Java中的checked exception和unchecked exception Java中有两种异常:checked exception和unchecked exception. checked exception checked exception是这样定义: A checked exception is an exception that must be either caught or declared in a method where it can be thrown. 也就是

几种常见的Runtime Exception

摘要:一,error和exception的区别,RuntimeException和非RuntimeException的区别1.异常机制      异常机制是指当程序出现错误后,程序如何处理.具体来说,异常机制提供了程序退出的安全通道.当出现错误后,程序执行的流程发生改变,程序的控制权转移到异常处理器.     传统的处理异常的办法是,函数返回一个特殊的结果来表示出现异常(通 一,error和exception的区别,RuntimeException和非RuntimeException的区别 1.

Co-variant array conversion from x to y may cause run-time exception

http://stackoverflow.com/questions/8704332/co-variant-array-conversion-from-x-to-y-may-cause-run-time-exception What it means is this Control[] controls = new LinkLabel[10]; // compile time legal controls[0] = new TextBox(); // compile time legal, ru

最常见到的runtime exception:

最常见到的runtime exception:ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackE

java中的Checked Exception和Unchecked Exception的区别

Java 定义了两种异常: - Checked exception: 继承自 Exception 类是 checked exception.代码需要处理 API 抛出的 checked exception,要么用 catch 语句,要么直接用 throws 语句抛出去. - Unchecked exception: 也称 RuntimeException,它也是继承自 Exception.但所有 RuntimeException 的子类都有个特点,就是代码不需要处理它们的异常也能通过编译,所以它

异常处理—checked exception 和 unchecked exception

异常的Root Class是Throwable,Throwable派生了Error和Exception. Java 8 API Doc中对checked exception和unchecked exception 的说明: 1. checked exception:(在Exception类中的说明) The class Exception and any subclasses that are not also subclasses of RuntimeException are checked

第10章-异常处理 --- Checked异常和Runtime异常体系

第10章-异常处理 --- Checked异常和Runtime异常体系 Java的异常被分为两大类:Checked异常和Runtime异常(运行时异常).所有的RuntimeException类及其子类的实例被称为Runtime异常;不是RuntimeException类及其子类的异常实例则被称为Checked异常. 对于Checked异常的处理方法有如下两种: (1)当前方法明确知道如何处理该异常,程序应该用try...catch块来捕获该异常,然后在对应的catch块中修复该异常. (2)当

抛出了无数的Exception,但是Exception到底是啥?解开Exception的神秘面纱...

java异常 什么是异常呢? 定义:当一个程序在运行过程中,出现了一些非正常执行流程的指令,那么就会产生一个事件对象,这个事件对象在java就简称为异常(Exception). An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. 异常Handler: 当一个异常出现了,那么jvm就