美文网首页
java中的异常

java中的异常

作者: 泰兰德的加长香蕉 | 来源:发表于2018-10-04 11:51 被阅读0次
java异常

  Notice that all exceptions descend from Throwable, but the hierarchy immediately splits into two branches: Error and Exception.

  The Error hierarchy describes internal errors and resource exhaustion situations inside the Java runtime system. You should not throw an object of this type. There is little you can do if such an internal error occurs, beyond notifying the user and trying to terminate the program gracefully. These situations are quite rare.

When doing Java programming, you focus on the Exception hierarchy. The Exception hierarchy also splits into two branches: exceptions that derive from RuntimeException and those that do not.

The general rule is this: A RuntimeException happens because you made a programming error. Any other exception occurs because a bad thing, such as an I/O error, happened to your otherwise good program.

Exceptions that inherit from RuntimeException include such problems as

• A bad cast

• An out-of-bounds array access

• A null pointer access

Exceptions that do not inherit from RuntimeException include

• Trying to read past the end of a file

• Trying to open a file that doesn’t exist

• Trying to find a Class object for a string that does not denote an existing class

  The rule “If it is a RuntimeException, it was your fault” works pretty well. You could have avoided that ArrayIndexOutOfBoundsException by testing the array index against the array bounds. The NullPointerException would not have happened had you checked whether the variable was null before using it.

  How about a file that doesn’t exist? Can’t you first check whether the file exists, and then open it? Well, the file might be deleted right after you checked for its existence. Thus, the notion of “existence” depends on the environment, not just on your code.

  The Java Language Specification calls any exception that derives from the class Error or the class RuntimeException an unchecked exception. All other exceptions are called checked exceptions. This is useful terminology that we also adopt. The compiler checks that you provide exception handlers for all checked exceptions.

相关文章

  • Java基础_异常

    本文主要从如下几点学习Java中的异常 异常的分类 异常分类结构图 异常处理的方法 异常的分类 Java库中本身内...

  • Java异常简介及其架构

    Java异常简介 Java异常是Java提供的一种识别及响应错误的一致性机制。Java异常机制可以使程序中异常处理...

  • java异常体系及1.7中的try-with-resources

    1.java异常体系架构 异常指java运行过程出现的错误,在java中,将异常当作对象来处理,java.lang...

  • Java(六) 异常

    Java 中的异常处理 在Java 中 通过 Throwable及其子类描述各种不同的异常类型 Throwab...

  • 第34节:Java当中的异常

    Java当中的异常 了解Java当中的异常,那么什么是异常呢?异常又有什么分类呢?异常中的特殊结构:try...c...

  • 崩溃优化笔记

    Java崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获异常,导致程序异常退出 Native崩溃 代码中...

  • 异常

    一、Java中异常 1、Java中的异常分为两大类:Checked Exception(非Runtime Exce...

  • Java基础语法_Day11

    一、异常产生&异常处理 异常概述 什么是异常?Java代码在运行时期发生的问题就是异常。在Java中,把异常信息封...

  • java异常机制

    异常是什么? 第一,异常模拟的是现实世界中“不正常的”事件。 第二,java中采用”类“去模拟异常。java的异常...

  • Java基础之异常处理

    Java基础之异常处理 在Java程序中,异常分为编译异常以及运行时异常 编译异常:程序在编译期间就出现的异常,必...

网友评论

      本文标题:java中的异常

      本文链接:https://www.haomeiwen.com/subject/fwlxaftx.html