目标
java异常概念
异常的体系
异常处理
throws和throw
自定义异常
一、java异常概念
1、异常:不正常的情况
2、广义上的异常:程序运行时所出现的所有不正常情况
错误(Error):java虚拟机无法解决的严重问题(文件读写时文件删除、网络中断、内存不够。)一般不编写针对性的代码进行处理。
3、狭义上的异常:运行时,由于程序缺陷导致在运行时出现不正常情况,出现后程序可以控制,进行处理,程序继续执行。(Exception):数组越界、数字格式化、空指针、类类型转换、算数异常。
4、程序异常处理方式有:
(1)虚拟机终止(默认处理方式)。
(2)程序员在编写程序时,就考虑到异常的检测、异常消息的提示,以及异常的处理。
5、 捕获异常最理想的是在编译期间,但有的异常只有在运行时才会发生。(除数为0,数组下标越界等)
分类:编译时异常和运行时异常
1、数组索引越界 java.lang.ArrayIndexOutOfBoundsException
int []a=new int[5];
a[5]=2;
System.out.println(Arrays.toString(a));
2、空指针异常 java.lang.NullPointerException
String s=null;
s.length();
3、类型转换异常 java.lang.ClassCastException
Object obj="abc";
Integer i=(Integer)obj;
4、数字格式化异常 java.lang.NumberFormatException
new Integer("abc");
5、算数异常 java.lang.ArithmeticException
int a=10;
int b=0;
System.out.println(a/b);
二、异常的体系
1、Throwable类有两个直接子类:Exception类和 Error类。
2、异常分为运行期异常和编译期异常两种
三、异常处理
1、java编程语言使用异常处理机制为程序提供了错误处理的能力。
2、java的异常处理是通过五个关键字来实现的:try、catch、finally、throw、throws
3、基本语法
try{
可能会发生异常的代码
(try块中任何一条语句发生了异常,下面的代码将不会被执行,程序将
跳转到异常处理代码块中,即catch块。因此,不要随意将不相关的代
码放到try块中,因为随时可能会中断执行。)
}catch(异常类型 引用名){
异常处理代码
catch语句必须紧跟着try语句之后
}finally{
必须执行代码
}
4、try-catch
public static void main(String[] args) {
try {
int a=10;
int b=0;
System.out.println(a/b);
}catch (ArithmeticException e){
e.printStackTrace();//获取异常类名和异常信息,以及异常出现在程序中的位置。返回void
System.out.println(e.getMessage());//获取异常信息,返回字符串
System.out.println("算数异常错误");
}
}
try catch的嵌套写法
public static void main(String[] args) {
try {
int a=10;
int b=2;
System.out.println(a/b);
try {
String s=null;
s.length();
} catch (NullPointerException e) {
System.out.println("空指针异常");
}
}catch (ArithmeticException e){
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("算数异常错误");
}
}
在try中写多个可能出现异常的程序,在try方法外写多个catch语句
public static void main(String[] args) {
try {
int a=10;
int b=2;
System.out.println(a/b);
String s=null;
s.length();
}catch (ArithmeticException e){
// e.printStackTrace();
// System.out.println(e.getMessage());
System.out.println("异常错误");
}catch (NullPointerException e) {
System.out.println("空指针异常");
} catch (Exception e){
System.out.println("出错啦");
}
}
5、try-catch-finally
如果b=0; 执行catch中的语句,finally中的,和bbbbbbbbb;
如果b=2; 执行try中的句子,finally中的,和bbbbbbbbb;
public static void main(String[] args) {
try {
int a=10;
int b=0; //b=2;
int c=a/b;
}catch (ArithmeticException e){
System.out.println("aaaaaaaaaaaaaaaaa");
}finally {
System.out.println("cccccccccccc");
}
System.out.println("bbbbbbbbbbbb");
}
如果b=0; 执行catch中的语句,finally中的,返回-1;
如果b=2; 执行try中的句子,finally中的,返回1;
public static void main(String[] args) {
System.out.println(test());
}
public static int test() {
try {
int a = 10;
int b = 0;
int c = a / b;
return 1;
} catch (ArithmeticException e) {
System.out.println("aaaaaaaaaaaaaaaaa");
return -1;
} finally {
System.out.println("cccccccccccc");
// return 0;
}
}
6.try-finally
不论try中程序是否出现异常,输出为ccccccccccccc 和返回-1;
public static void main(String[] args) {
System.out.println(test());
}
public static int test() {
try {
int a = 10;
int b = 2;
int c = a / b;
return 1;
} finally {
System.out.println("cccccccccccc");
return -1;
}
}
四、throws
1、throws,定义一个方法的时候可以用throws关键字声明,表示此方法不处理异常,而是交给方法调用处进行处理。
2、使用throws的方法,调用时必须处理声明的异常,要么使用try-catch,要么继续使用throws声明。
public static void main(String[] args) {
try {
test1();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private static void test1() throws UnsupportedEncodingException {
test2();
}
public static void test2() throws UnsupportedEncodingException {
test3();
}
public static void test3() throws UnsupportedEncodingException ,NullPointerException{
String s="中国";
s.getBytes("gbk");
}
3、任何方法都可以使用throws关键字声明异常类型,包括抽象方法
public abstract class ExceptionDemo8 {
/*
重写:方法名 数据类型相同,
子类权限大于等于父类
子类不允许实现异常大于等于父类
*/
public abstract void test() throws Exception;
}
五、throw
1、throw关键字用于显示抛出异常,抛出的时候是抛出的是一个异常类的实例化对象。
2、在异常处理中,try语句要捕获的是一个异常对象,那么此异常对象也可以自己抛出。
public static void main(String[] args) {
try {
System.out.println(typeFlie(null));
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
public static String typeFlie(String filename){
if(filename==null){
throw new NullPointerException("文件出错啦");
}
return filename.substring(filename.indexOf(".")+1);
}
六、throws和throw
1、throws用于方法声明处,用来声明该方法可能发生的异常类型,可以是多个异常类型,用来强制调用该方法时处理这些异常。
2、throw用于方法体中,用来抛出一个实际的异常对象,使用throw后,要么用try-catch捕获异常,要么使用throws声明异常。
3、抽象方法也可以使用throws。
七、自定义异常
1、自定义异常就是自己定义的异常类,也就是API中的标准异常类的直接或间接的子类。
2、作用:用自定义异常标记业务逻辑的异常,避免与标准异常混淆。
public class SoreException extends Exception {
//自定义异常类中往往不写其他方法,只重载需要使用的构造方法
//继承Exception,在方法中使用throw抛出后,必须在方法中try-catch或throws抛出
public SoreException() {
super();
}
public SoreException (String message){
super(message);
}
}
public class Sore {
public static void main(String[] args) {
try {
sores(-10);
} catch (SoreException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
public static int sores(int i) throws SoreException {
if(i<10){
throw new SoreException("成绩错误");
}if(i>100){
throw new SoreException("成绩错");
}else {
return 1;
}
}
}