JVM无法解决的严重问题,如资源耗尽等严重情况,StackOverflowError和OOM(OutOfMemory)。
源耗尽1)栈溢出
// 递归调用
public static void main(String[] args) {
main(args);
}
栈溢出异常:
Exception in thread "main" java.lang.StackOverflowError
2)堆溢出
Integer[] arr = new Integer[1024*1024*1024]; 堆溢出异常: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space2. Exception(违例)
因编程错误或偶然的外在因素导致的一般性问题。
2.1 编译时异常public void test1(){
File file = new File("abc.txt");
// Unhandled exception: java.io.FileNotFoundException
FileInputStream fis = new FileInputStream(file);
// Unhandled exception: java.io.IOException
int data = fis.read();
while (data != -1){
System.out.println((char)data);
}
// Unhandled exception: java.io.IOException
fis.close();
}
2.2 运行时异常
1)空指针异常
public void test1(){
int[] arr = null;
System.out.println(arr[0]);
}
空指针异常:
java.lang.NullPointerException
2)角标越界异常
public void test2(){
int[] arr = new int[3];
System.out.println(arr[3]);
}
数组角标越界异常:
java.lang.ArrayIndexOutOfBoundsException: 3
3)类型转换异常
public void test3(){
Object date = new Date();
String dateStr = (String)date;
}
类型转换异常:
java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String
4)数字格式异常
public void test4(){
String name = "chengyu";
Integer.valueOf(name);
}
数字格式异常:
java.lang.NumberFormatException: For input string: "chengyu"
5)算术异常
public void test5(){
int a = 10/0;
}
算术异常:
java.lang.ArithmeticException: / by zero
3. 异常处理方式
3.1 抛出
没有解决异常问题,只是抛给上一级,让上一级解决;
throws + 异常类型
@Test
public void test4() throws NumberFormatException{
String name = "chengyu";
Integer.valueOf(name);
}
@Test
public void test5(){
try {
test4();
}catch (NumberFormatException e){
System.out.println(e.getMessage());;
}
}
3.2 捕获
3.2.1 格式
try{
可能出现问题的代码
}catch(异常类型1 变量名1){
处理异常的代码
}catch(异常类型2 变量名2){
处理异常的代码
}catch(异常类型3 变量名3){
处理异常的代码
}finally{
无论有无异常都执行的代码
}
3.2.2 例子
public void test4(){
try {
String name = "chengyu";
Integer.valueOf(name);
}catch (NumberFormatException e){
System.out.println(e.getMessage());
// 输出:For input string: "chengyu"
e.printStackTrace();
}finally {
}
}
3.2.3 捕获异常后的提示
1)e.getMessage()
打印异常信息
System.out.println(e.getMessage());
输出:
For input string: “chengyu”
2)e.printStackTrace()
打印详细异常信息
输出:
java.lang.NumberFormatException: For input string: "chengyu" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.valueOf(Integer.java:766) ... at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)3.3 手动抛出异常
throw New 异常类型(“异常信息”);
@Test
public void test4() throws Exception {
throw new Exception("出异常了!");
}
@Test
public void test5(){
try {
test4();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
输出:
出异常了!
3.4 自定义异常
1)继承Exception 或RuntimeException;
2)提供全局常量(序列化标识用):serialVersionUID;
3)重载的构造器。
public class MyExceptionTest{
@Test
public void test(){
throw new MyException("MyException Test!");
}
}
// 自定义异常类
class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766939L;
public MyException() {
}
public MyException(String message) {
super(message);
}
}
3.5 注意
1)没有finally 时,捕获到异常后,跳出当前方法;有finally 时,执行finally 中代码再跳出;
2)catch 异常类不可以先写父类再写子类;
3)子类重写的方法抛出的异常不能大于父类方法抛出的异常;
因为在使用多态时,调用方法捕获父类异常,若子类异常大于父类异常,父类无法捕获
public class OverrideTest {
public static void main(String[] args) {
OverrideTest test = new OverrideTest();
// 多态的应用,调用的是子类的method,若子类异常大于父类异常,show 方法将无法捕获
test.show(new SubClass());
}
public void show(SuperClass s){
try {
s.method();
// 捕获的是父类的异常
} catch (IOException e) {
e.printStackTrace();
}
}
}
class SuperClass{
public void method() throws IOException{
}
}
class SubClass extends SuperClass{
@Override
public void method() throws FileNotFoundException {
}
}
3.6 开发中如何选择异常处理方式
1)如果父类中被重写的方法没有抛异常,则子类重写的方法且可能存在异常时,必须用捕获异常的方式处理;
2)执行的方法内,先后又调用了另外的几个方法,且递进关系,建议内部的几个方法使用抛出异常,而外部方法用捕获异常的方式进行处理;



