Java中的异常包括错误和异常
Throwable 是所有异常和错误的父类,错误的父类是Error,应用程序不应该试图捕获的严重问题,也就是无法挽救的问题。异常的父类是Exception,应用程序可以捕获的问题,也就是有办法扑救的问题
1,异常的分类:异常分为编译异常和运行时异常,编译时异常是在编译程序的时候需要强制处理的异常,运行时异常(RuntimeException)在编译程序的时候不需要强制处理的异常,但可以自己检查出来。
2,解决异常的方法:可以使用try…..catch…finally块来处理 一旦在try中发生异常,就立马放弃执行,转而执行catch中的代码,catch中会封装一个异常对象,finally,是一定执行。也可以使用throws XXXException,用于出现异常的方法
如下代码:
public class Demo {
public static void main(String[] args) {
Demo d = new Demo();
try {
d.checkNum(100);
} catch (Exception ef) {
ef.printStackTrace();
} finally {
// 不管是在执行try还是catch,都会执行finally
// 一般情况下,finally主要是用来对try...catch进行后期的处理,比如释放占用的资源
}
// d.check(101);
int t = d.test(100);
System.out.println(t);
}
public int test(int t) {
try {
check(t);
return 0;
} catch (Exception ef) {
ef.printStackTrace();
return 1;
} finally {
System.out.println(">>>>>>>>>>>>");
}
}
// 抛出一个编译时异常
public void checkNum(int t) throws Exception {
if (t < 0 || t > 100) {
// 创建一个异常对象
Exception ep = new Exception("数值超出范围!!");
// 抛出异常对象
throw ep;
} else {
System.out.println(t + "是在正常范围内!!");
}
}
// 抛出运行时异常
public void check(int t) {
if (t < 0 || t > 100) {
// 创建一个运行时异常
FeiFaDeXiaBiaoException ep = new FeiFaDeXiaBiaoException(
"数值超出范围!!!");
throw ep;
} else {
System.out.println(t + "是在正常范围内!!");
}
}
}
常见的异常:
编译时异常:
InterruptedException 线程中断异常
ClassNotFoundException 类没有找到的异常
DataFormatException 数据格式异常
IOException 输入输出异常
FileNotFoundException 找不到文件异常
CharacterCodingException 字符编码异常
SQLException 数据库操作异常
TimeoutException 阻塞操作超时异常
运行时异常:
ClassCastException 类型强制转换的异常
IllegalArgumentException 非法的参数异常
IndexOutOfBoundsException 下标越界异常
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
NullPointerException 空指针异常
ArithmeticException 除以0的时候的异常
InputMismatchException 输入不匹配的异常
NumberFormatException 数字格式化异常
Throwable 是所有异常和错误的父类,错误的父类是Error,应用程序不应该试图捕获的严重问题,也就是无法挽救的问题。异常的父类是Exception,应用程序可以捕获的问题,也就是有办法扑救的问题
1,异常的分类:异常分为编译异常和运行时异常,编译时异常是在编译程序的时候需要强制处理的异常,运行时异常(RuntimeException)在编译程序的时候不需要强制处理的异常,但可以自己检查出来。
2,解决异常的方法:可以使用try…..catch…finally块来处理 一旦在try中发生异常,就立马放弃执行,转而执行catch中的代码,catch中会封装一个异常对象,finally,是一定执行。也可以使用throws XXXException,用于出现异常的方法
如下代码:
public class Demo {
public static void main(String[] args) {
Demo d = new Demo();
try {
d.checkNum(100);
} catch (Exception ef) {
ef.printStackTrace();
} finally {
// 不管是在执行try还是catch,都会执行finally
// 一般情况下,finally主要是用来对try...catch进行后期的处理,比如释放占用的资源
}
// d.check(101);
int t = d.test(100);
System.out.println(t);
}
public int test(int t) {
try {
check(t);
return 0;
} catch (Exception ef) {
ef.printStackTrace();
return 1;
} finally {
System.out.println(">>>>>>>>>>>>");
}
}
// 抛出一个编译时异常
public void checkNum(int t) throws Exception {
if (t < 0 || t > 100) {
// 创建一个异常对象
Exception ep = new Exception("数值超出范围!!");
// 抛出异常对象
throw ep;
} else {
System.out.println(t + "是在正常范围内!!");
}
}
// 抛出运行时异常
public void check(int t) {
if (t < 0 || t > 100) {
// 创建一个运行时异常
FeiFaDeXiaBiaoException ep = new FeiFaDeXiaBiaoException(
"数值超出范围!!!");
throw ep;
} else {
System.out.println(t + "是在正常范围内!!");
}
}
}
常见的异常:
编译时异常:
InterruptedException 线程中断异常
ClassNotFoundException 类没有找到的异常
DataFormatException 数据格式异常
IOException 输入输出异常
FileNotFoundException 找不到文件异常
CharacterCodingException 字符编码异常
SQLException 数据库操作异常
TimeoutException 阻塞操作超时异常
运行时异常:
ClassCastException 类型强制转换的异常
IllegalArgumentException 非法的参数异常
IndexOutOfBoundsException 下标越界异常
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
NullPointerException 空指针异常
ArithmeticException 除以0的时候的异常
InputMismatchException 输入不匹配的异常
NumberFormatException 数字格式化异常