指的是程序在执行过程中,出现的非正常的情况,最终会导致JVM的非正常停止。Java处 理异常的方式是中断处理。
异常的根类是 java.lang.Throwable,,其下有两个子类:java.lang.Error 与 java.lang.Exception。
编译时期异常:checked异常
。在编译时期,就会检查,如果没有处理异常,则编译失败。(如日期格式化异常)运行时期异常:runtime异常
。在运行时期,检查异常.在编译时期,运行异常不会编译器检测(不报错)。(如数学异常)Java异常处理的五个关键字:try、catch、finally、throw、throws
使用格式:
throw new 异常类名(参数);
public class ExceptionDemo {
public static int getElement(int[] arr , int index){
if (index < 0 || index >= arr.length){
throw new ArrayIndexOutOfBoundsException("数组下标越界啦!");
}
int element = arr[index];
return element;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int index = 5;
int element = getElement(arr, index);
System.out.println(element);
System.out.println("over");
}
}
使用格式:
修饰符 返回值类型 方法名(参数) throws 异常类名1,异常类名2…{ }
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionDemo {
public static void findFile(String file) throws FileNotFoundException,IOException{
if (!file.equals("a.txt")){
throw new FileNotFoundException("找不到文件");
}
int index = file.lastIndexOf(".");
String prefix = file.substring(index);
if (prefix.equals(".txt")){
System.out.println("文件格式正确");
}else {
throw new IOException("文件格式不正确");
}
}
public static void main(String[] args) throws IOException {
String file = "a.txt";
findFile(file);
System.out.println("over");
}
}
Java中对异常有针对性的语句进行捕获,可以对出现的异常进行指定方式的处理。
try{
编写可能会出现异常的代码
}catch(异常类型 e){
处理异常的代码
//记录日志/打印异常信息/继续抛出异常
}
import java.io.IOException;
public class ExceptionDemo {
public static void findFile(String file) throws IOException{
int index = file.lastIndexOf(".");
String prefix = file.substring(index);
if (prefix.equals(".txt")){
System.out.println("文件格式正确");
}else {
throw new IOException("文件格式不正确");
}
}
public static void main(String[] args){
String file = "a.html";
try {
findFile(file);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
System.out.println("over");
}
}
Throwable类中定义了一些查看方法:
有一些特定的代码无论异常是否发生,都需要执行。因此我们就会使用finally关键字来解决这个问题
import java.io.IOException;
public class ExceptionDemo {
public static void findFile(String file) throws IOException{
int index = file.lastIndexOf(".");
String prefix = file.substring(index);
if (prefix.equals(".txt")){
System.out.println("文件格式正确");
}else {
throw new IOException("文件格式不正确");
}
}
public static void main(String[] args){
String file = "a.html";
try {
findFile(file);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
System.out.println("不管出不出现异常都要执行!");
}
System.out.println("over");
}
}
注意:
异常类如何定义:
// 业务逻辑异常
public class MyException extends Exception {
/**
* 空参构造
*/
public MyException () {
}
/**
*
* @param message 表示异常提示
*/
public MyException (String message) {
super(message);
}
}
public class MyException extends RuntimeException {
/**
* 空参构造
*/
public MyException () {
}
/**
*
* @param message 表示异常提示
*/
public MyException (String message) {
super(message);
}
}
总的来说就是,继承异常类Exception或者RuntimeException,重写空参构造方法和有参构造方法。
欢迎java热爱者了解文章,作者将会持续更新中,期待各位友友的关注和收藏,另外对编程感兴趣的友友们可以加以下群共同学习。群号:1278716
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuoyibo.net 版权所有 湘ICP备2023021910号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务