概述
java未检查异常
已检查与未检查异常 (Checked vs UnChecked Exception)
Here, we will see how Checked Exception differs from UnChecked Exception?
在这里,我们将看到Checked Exception与UnChecked Exception有何不同?
检查异常 (Checked Exception)
Checked Exceptions are the exceptions which will be checked during compile time.
Checked Exceptions是在编译期间将检查的异常。
Checked Exceptions are all those exceptions which require try-catch block handling or throws keywords to specify Exception during compile time.
Checked Exception是所有需要try-catch块处理或在编译期间抛出关键字以指定Exception的异常。
In the case of Checked Exceptions, if the compiler does not find try-catch block handling then the compiler does not throw any compilation error, but an exception will be unreported and to solve this problem we need to use either try-catch or throws.
在检查异常的情况下,如果编译器未找到try-catch块处理,则编译器不会引发任何编译错误,但是将不报告异常,并且要解决此问题,我们需要使用try-catch或throws。
We will see which predefined exceptions are Checked Exceptions (i.e. Checked Exceptions are all those exceptions which are child class of Exception class directly but we need to remember that child class must not be inherited from RuntimeException [i.e. Any child class of Exception must not be a child class of RuntimeException]).
我们将看到哪些预定义的异常是Checked Exceptions(即Checked Exceptions是所有直接属于Exception类的子类的异常,但我们需要记住,子类不能从RuntimeException继承[ie Exception的任何子类都不能是RuntimeException的子类])。
All file related input/output exception comes under I/O Exception which will be Checked Exception.
所有与文件相关的输入/输出异常都在“ I / O异常”下,该异常将被称为“检查异常”。
We should go for the Checked Exception when the chances of failure of the code are higher during the operations.
当操作期间代码失败的机会更高时,我们应该选择Checked Exception。
We will see a few examples of Checked Exceptions, which are given below,
我们将看到一些检查异常的示例,如下所示,
IOException
IOException
SQLException
SQLException
ClassNotFoundException, etc
ClassNotFoundException等
Example:
例:
// Java program to demonstrate the example of
// Checked Exception
public class CheckedException {
public static void main(String[] args) throws Exception {
System.out.println("Exception will raise during compile-time");
}
}
Note: To save java file with different name and run with the class name
注意:要使用其他名称保存Java文件并以类名称运行
Output
输出量
Main.java:4: error: class CheckedException is public,
should be declared in a file named CheckedException.java
public class CheckedException {
^
1 error
Here, we will see how UnChecked Exception differs from Checked Exception?
在这里,我们将看到UnChecked Exception与Checked Exception有何不同?
未经检查的异常 (UnChecked Exception)
UnChecked Exceptions are the exceptions which will not be checked during the compile time.
未检查的异常是在编译期间不会检查的异常。
UnChecked Exceptions are all those exceptions which do not require try-catch block handling or throws during compile time.
未检查的异常是所有不需要try-catch块处理或在编译期间引发的异常。
In the case of UnChecked Exceptions, if the compiler does not find try-catch block handling then the compiler will not throw a compilation error.
对于UnChecked异常,如果编译器未找到try-catch块处理,则编译器将不会引发编译错误。
We will see which predefined exceptions are UnChecked Exceptions (i.e. UnChecked Exceptions are all those exceptions which are the child class of RuntimeException class directly i.e. we need to remember that child class must be inherited RuntimeException [i.e. Every child class of RuntimeException is UnChecked Exception]).
我们将看到哪些预定义的异常是未检查的异常(即所有未检查的异常都是直接作为RuntimeException类的子类的那些异常,即我们需要记住该子类必须继承RuntimeException [即,RuntimeException的每个子类都是UnChecked Exception]) 。
The Unchecked Exception mostly occurs due to programming errors or syntactical or logical errors and these mistakes will be done by the programmer.
Unchecked Exception通常是由于编程错误或语法或逻辑错误而发生的,这些错误将由程序员来完成。
We should go for UnChecked Exception when the chances of failure of programming mistakes are higher during syntax, logics like try to access null object, passing an illegal argument, access an element out of an array bound, etc.
当语法错误导致编程错误失败的可能性更高,尝试访问空对象,传递非法参数,访问数组边界之外的元素等逻辑时,我们应该选择UnChecked Exception。
We will see a few examples of UnChecked Exceptions, which are given below,
我们将看到一些未检查的异常示例,如下所示,
ArrayIndexOutofBoundsException
ArrayIndexOutofBoundsException
NullPointerException
空指针异常
IllegalArgumentException, etc
IllegalArgumentException等
Example:
例:
// Java program to demonstrate the example
// of UnChecked Exception
public class UnCheckedException {
public static void main(String[] args) {
int a = 10;
int b = 0;
int c = a / b;
System.out.println("The value of c is :" + c);
}
}
Output
输出量
Exception in thread "main" java.lang.ArithmeticException: / by zero
at UnCheckedException.main(UnCheckedException.java:9)
翻译自: https://www.includehelp.com/java/checked-vs-unchecked-exception.aspx
java未检查异常
最后
以上就是腼腆斑马为你收集整理的java未检查异常_Java中检查和未检查异常之间的区别的全部内容,希望文章能够帮你解决java未检查异常_Java中检查和未检查异常之间的区别所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复