标签:10 return 复习 finally names catch java 异常 public
异常
执行过程中所发生的异常事件可分为两大类
- Error(错误):java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。比如:StackOverflowError[栈溢出]和OOM(out of memory),Error是严重错误,程序会崩溃
- Exception:其它因编程错误或偶然的外在因素导致的一致性问题,可以使用针对性的代码进行处理。例如空指针访问,视图读取不存在的文件,网络连接中断等等,Exception分为两大类:运行时异常[程序运行时发生的异常] 和编译时异常 [编程时,由编译器检查出的异常]
受检异常和非受检异常
Error和java.lang.RuntimeException及其子类为非受检异常,除了非受检异常都是受检异常
try-catch异常处理注意
- 可以有多个catch语句捕获不同的异常(进行不同的业务处理),要求父类异常在后,子类异常在前,如果发生异常,只会蒲培一个catch
try-catch-finally练习
练习1
public class Exception01 {
public static int method() {
try {
String[] names = new String[3];
if (names[1].equals("tom")) {
System.out.println(names[1]);
} else {
names[3] = "hspedu";
}
return 1;
} catch (ArrayIndexOutOfBoundsException e) {
return 2;
} catch (NullPointerException e) {
return 3;
} finally {
return 4;
}
}
public static void main(String[] args) {
System.out.println(method());//4
}
}
练习2
public class TryCatchExercise {
public static void main(String[] args) {
System.out.println(method());
}
public static int method() {
int i = 1;
try {
i++; // i = 2
String[] names = new String[3];
if (names[1].equals("tom")) { // 空指针异常
System.out.println(names[1]);
} else {
names[3] = "hspedu";
}
return 1;
} catch (ArrayIndexOutOfBoundsException e) {
return 2;
} catch (NullPointerException e) {
return ++i;
} finally {
return ++i;
}
}
public static void main(String[] args) {
System.out.println(method());//4
}
}
练习3
注意返回值,如果有finally,返回值会以临时变量的形式存储,catch异常后的return的结果会保存在一个临时变量中,此时会继续执行finally,之后再返回这个临时变量3 => i = 4;3
- 如果没有出现异常,则执行try块中所有语句,不执行catch块中语句,如果有finally,最后还需要执行finally里面的语句
- 如果出现异常,则try块中异常发生后剩下的语句不再执行。将执行catch块中的语句,如果有finally,最后还需要执行finally中的语句
- 总结空指针异常中的返回不会立即返回,临时变量存储,finally中没有return,正常执行后,返回上一步的临时变量
public class TryCatchExercise {
public static void main(String[] args) {
System.out.println(method());
}
public static int method() {
int i = 1;
try {
i++; // i = 2
String[] names = new String[3];
if (names[1].equals("tom")) { // 空指针异常
System.out.println(names[1]);
} else {
names[3] = "hspedu";
}
return 1;
} catch (ArrayIndexOutOfBoundsException e) {
return 2;
} catch (NullPointerException e) {
return ++i; // i = 3 => 保存临时变量 temp = 3;
} finally {
++i;
System.out.println("i = " + i); // i = 4
}
}
}
throws异常处理
- 如果一个方法可能发生某种异常,但是该并不能确定如何处理这种异常,则次方法应显式的声明抛出异常,由调用方处理
- 在方法声明中用throws语句可以声明抛出异常的列表,throws后面的异常类型可以时方法产生的异常类型,也可以时它的父类
- 对于运行时异常,程序如果没有处理,默认就是throws的方式处理
- 子类重写父类的方法时,对抛出的异常的规定:子类重写的方法,所抛出的异常类型要么和父类抛出的异常一致,要么为父类抛出的异常的类型的子类型
自定义异常
- 定义类:自定义异常类名继承Exception或RuntimeException
- 如果继承Exception,属于编译异常
- 如果继承RuntimeException,属于运行时异常(一般来说,继承RuntimeException)
public class CustomException {
public static void main(String[] args) {
int age = 180;
if (!(age >= 18 && age <= 120)) {
throw new AgeException("年龄需要在18~120之间");
}
System.out.println("年龄正常");
}
}
class AgeException extends RuntimeException {
public AgeException(String message) {
super(message);
}
}
- throw throws区别
| | 意义 | 位置 | 后面跟的东西 |
| ----------- | ------------ | ----------- | ----------- |
| throws | 异常处理的一种方式 |方法声明处 | 异常类型|
| throw | 手动生成异常对象的关键字 |方法体中 |异常对象 |
标签:10,return,复习,finally,names,catch,java,异常,public 来源: https://www.cnblogs.com/zyyanga/p/16069969.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。