`
swift123
  • 浏览: 19249 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论
  • yashiro_w: 15w年薪编程语言与框架 不问设计模式,不看是否通过OCJP, ...
    面试技巧

Checked vs Unchecked Exceptions in Java

 
阅读更多

1) Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword

 

2) Unchecked are the exceptions that are not checked at compiled time. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to be civilized, and specify or catch the exceptions.
In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.

 

Should we make our exceptions checked or unchecked?
If a client can reasonably be expected to recover from an exception, make it a checked exception.

If a client cannot do anything to recover from the exception, make it an unchecked exception

分享到:
评论

相关推荐

    Bug模式 - Bug Pattern In Java

    Java区分了受检异常(Checked Exceptions)和未受检异常(Unchecked Exceptions)。未捕获的未受检异常可能导致程序突然终止。良好的编程习惯是为可能出现的未受检异常提供处理代码,即使只是一个简单的日志记录。 ...

    java english interview

    * Java 定义了两种类型的异常:checked exceptions 和 unchecked exceptions。 * Checked exceptions:继承自 Exception 类的异常,客户端代码必须处理这些异常,可以在 catch 子句中捕获或使用 throws 子句将其传递...

    Java语言程序设计基础篇课后题答案-Chapter17ExceptionsandAssertions.pdf

    本资源对Java语言程序设计基础篇的Chapter 17 Exceptions and Assertions进行了详细的解释和知识点总结,涵盖了Java异常类的继承结构、claiming exceptions、checked exception和unchecked exception、throw语句和...

    麻省理工18年春软件构造课程阅读06-规格说明-1

    首先,文档对比了Java中的checked和unchecked exceptions。Checked exceptions(如IOException)是编译时需要处理的异常,而unchecked exceptions(如NullPointerException)则在运行时检查。在Java中,程序员通常...

    Java-CustomExceptions:Java 自定义异常

    Java异常分为两种类型:未检查异常(Unchecked Exceptions)和检查异常(Checked Exceptions)。自定义异常如果继承自`RuntimeException`或其子类,将成为未检查异常,否则默认为检查异常。未检查异常在编译时不强制...

    Java的内置异常-Java教程共1页.pdf.zip

    Java将异常分为两大类:检查性异常(Checked Exceptions)和运行时异常(Unchecked Exceptions)。检查性异常是那些在编译期间需要处理的异常,如IOException,而运行时异常则是在程序运行期间抛出的,例如...

    java常用API.pdf

    异常分为受检异常(checked exceptions)和非受检异常(unchecked exceptions)。Java使用try-catch-finally语句块来捕获和处理异常。 JDBC(Java Database Connectivity)是Java中用于连接和操作数据库的标准接口...

    JAVA思维导图9张!

    3. **异常处理**:这部分可能会讲解如何使用try-catch-finally语句来捕获和处理异常,以及不同类型的异常类,如检查异常(Checked Exceptions)和运行时异常(Unchecked Exceptions)。 4. **集合框架**:Java集合...

    Java多线程编程实战指南-核心篇

    而检查异常(Checked Exceptions)如果在线程中抛出,需在该线程或其祖先线程中捕获,否则会导致线程中断。 最后,书中还将涵盖Java内存模型(JMM)和volatile关键字。JMM定义了线程如何访问共享变量的规则,保证了...

    JAVA语言规范 每个JAVA程序员都该读的文档

    它也提到了受检查异常(Checked Exceptions)和未检查异常(Unchecked Exceptions)的区别。 内存管理和垃圾回收也是Java的一大特色。规范中详细阐述了对象的生命周期,包括创建、引用、垃圾收集和内存泄漏的预防。...

    Java经典笔试题收集.docx

    - **Checked Exceptions与Unchecked Exceptions的区别** - `Checked Exceptions`: 在编译时进行检查,必须处理或者声明抛出。 - `Unchecked Exceptions`: 在运行时发生,通常是程序逻辑错误,不应该被程序捕获,...

    java学习 java学习

    学习如何使用try-catch-finally结构来处理异常,以及了解不同类型的异常,如检查异常(Checked Exceptions)和运行时异常(Unchecked Exceptions),对于编写健壮的代码至关重要。 Java的GUI编程可以使用Swing或...

    深入理解java异常处理机制Java开发Java经验技巧共

    还有可检查异常(Checked Exceptions)和运行时异常(Unchecked Exceptions)的区别。前者是必须在方法签名中声明或在方法体内捕获的异常,如IOException;后者通常是编程错误,如NullPointerException,它们不需要...

    java异常类型.txt

    在Java中,异常被分为两大类:检查性异常(Checked Exceptions)与非检查性异常(Unchecked Exceptions)。下面我们将详细介绍这些异常类别及具体的异常类型。 ### 检查性异常 (Checked Exceptions) 这类异常是...

    java基础PPTJava基础PPT

    异常类继承自java.lang.Throwable,分为检查异常(Checked Exceptions)和运行时异常(Unchecked Exceptions)。良好的异常处理能够使程序更加健壮,易于调试。 此外,PPT可能还会涉及线程和并发编程。Java内置了对...

    java中常用的异常类型共2页.pdf.zip

    这些异常分为两大类:检查性异常(Checked Exceptions)和运行时异常(Unchecked Exceptions)。检查性异常通常是由外部因素引起的,如文件不存在、网络连接问题等,这些异常在编译时期就需要进行处理。运行时异常则...

    java学习之Java异常.ppt

    异常可以分为两大类:已检查异常(checked exceptions)和未检查异常(unchecked exceptions)。已检查异常是那些在编译期间需要显式处理的异常,例如`IOException`,而未检查异常通常是编程错误,如空指针异常`...

    深入理解java异常处理

    2. **运行时异常**(Unchecked Exceptions):在运行时由Java虚拟机自动抛出的异常,无需在编译时捕获或声明抛出。 #### 三、异常的抛出与捕获 - **异常抛出**:当Java程序执行某个操作时,如果出现错误,系统会...

    JAVA讲义第6章.pdf

    异常分为两大类:受检异常(checked exceptions)和非受检异常(unchecked exceptions)。 ##### 受检异常(Checked Exceptions) 这些异常必须被捕获或声明,例如`IOException`、`ClassNotFoundException`和`...

Global site tag (gtag.js) - Google Analytics