java异常处理_Java异常处理

java异常处理

Exception Handling is a mechanism to handle exception at runtime. Exception is a condition that occurs during program execution and lead to program termination abnormally. There can be several reasons that can lead to exceptions, including programmer error, hardware failures, files that need to be opened cannot be found, resource exhaustion etc.

异常处理是一种在运行时处理异常的机制。 异常是在程序执行期间发生并导致程序异常终止的情况。 导致异常的原因可能有多种,包括程序员错误,硬件故障,无法打开需要打开的文件,资源耗尽等。

Suppose we run a program to read data from a file and if the file is not available then the program will stop execution and terminate the program by reporting the exception message.

假设我们运行一个程序从文件中读取数据,如果该文件不可用,则该程序将停止执行并通过报告异常消息来终止该程序。

The problem with the exception is, it terminates the program and skip rest of the execution that means if a program have 100 lines of code and at line 10 an exception occur then program will terminate immediately by skipping execution of rest 90 lines of code.

异常的问题是,它将终止程序并跳过其余的执行,这意味着如果程序有100行代码,并且在第10行发生异常,则程序将通过跳过其余的90行代码立即终止。

To handle this problem, we use exception handling that avoid program termination and continue the execution by skipping exception code.

为了解决此问题,我们使用异常处理来避免程序终止,并通过跳过异常代码来继续执行。

Java exception handling provides a meaningful message to the user about the issue rather than a system generated message, which may not be understandable to a user.

Java异常处理向用户提供有关该问题的有意义的消息,而不是系统生成的消息(用户可能无法理解)。

未捕获的异常 (Uncaught Exceptions)

Lets understand exception with an example. When we don't handle the exceptions, it leads to unexpected program termination. In this program, an ArithmeticException will be throw due to divide by zero.

让我们以一个例子来了解异常。 当我们不处理异常时,它将导致程序意外终止。 在此程序中,由于被零除,将引发ArithmeticException。

class UncaughtException
{
 public static void main(String args[])
 {
  int a = 0;
  int b = 7/a;     // Divide by zero, will lead to exception
 }
}

This will lead to an exception at runtime, hence the Java run-time system will construct an exception and then throw it. As we don't have any mechanism for handling exception in the above program, hence the default handler (JVM) will handle the exception and will print the details of the exception on the terminal.

这将在运行时导致异常,因此Java运行时系统将构造一个异常,然后将其抛出。 由于上述程序中没有任何处理异常的机制,因此默认处理程序(JVM)将处理该异常,并将在终端上打印该异常的详细信息。

Uncaught Exception in java

Java异常 (Java Exception)

A Java Exception is an object that describes the exception that occurs in a program. When an exceptional events occurs in java, an exception is said to be thrown. The code that's responsible for doing something about the exception is called an exception handler

Java异常是一个对象,它描述程序中发生的异常。 当Java中发生异常事件时,据说会引发异常。 负责处理异常的代码称为异常处理程序

如何处理异常 (How to Handle Exception)

Java provides controls to handle exception in the program. These controls are listed below.

Java提供了处理程序中异常的控件。 这些控件在下面列出。

  • Try : It is used to enclose the suspected code.

    尝试:用来封装可疑代码。

  • Catch: It acts as exception handler.

    捕获:它充当异常处理程序。

  • Finally: It is used to execute necessary code.

    最后:用于执行必要的代码。

  • Throw: It throws the exception explicitly.

    抛出:它显式抛出异常。

  • Throws: It informs for the possible exception.

    抛出:通知可能的异常。

We will discuss about all these in our next tutorials.

我们将在接下来的教程中讨论所有这些内容。

异常类型 (Types of Exceptions)

In Java, exceptions broadly can be categories into checked exception, unchecked exception and error based on the nature of exception.

在Java中,根据异常的性质,异常可以大致分为检查异常,非检查异常和错误。

  • Checked Exception

    检查异常

  • The exception that can be predicted by the JVM at the compile time for example : File that need to be opened is not found, SQLException etc. These type of exceptions must be checked at compile time.

    JVM可以在编译时预测的异常, 例如 :找不到需要打开的文件,SQLException等。必须在编译时检查这些类型的异常。

  • Unchecked Exception

    未经检查的异常

  • Unchecked exceptions are the class that extends RuntimeException class. Unchecked exception are ignored at compile time and checked at runtime. For example : ArithmeticException, NullPointerException, Array Index out of Bound exception. Unchecked exceptions are checked at runtime.

    未检查的异常是扩展RuntimeException类的类。 未检查的异常在编译时被忽略,并在运行时检查。 例如 :ArithmeticException,NullPointerException,数组索引超出绑定异常。 在运行时检查未检查的异常。

  • Error

    错误

  • Errors are typically ignored in code because you can rarely do anything about an error. For example, if stack overflow occurs, an error will arise. This type of error cannot be handled in the code.

    错误通常在代码中被忽略,因为您几乎无法对错误做任何事情。 例如 ,如果发生堆栈溢出,则会出现错误。 此类错误无法在代码中处理。

Java异常类层次结构 (Java Exception class Hierarchy)

All exception types are subclasses of class Throwable, which is at the top of exception class hierarchy.

所有异常类型都是Throwable类的子类,它在异常类层次结构的顶部。

exception handling in java
  • Exception class is for exceptional conditions that program should catch. This class is extended to create user specific exception classes.

    异常类用于程序应捕获的异常条件。 扩展此类以创建用户特定的异常类。

  • RuntimeException is a subclass of Exception. Exceptions under this class are automatically defined for programs.

    RuntimeException是Exception的子类。 此类下的异常是为程序自动定义的。

  • Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself. Stack overflow is an example of such an error.

    Java运行时系统使用类型为Error的 异常来指示与运行时环境本身有关的错误。 堆栈溢出就是此类错误的一个示例。

翻译自: https://www.studytonight.com/java/exception-handling.php

java异常处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值