1.1、异常概述
1.2、JVM Java虚拟机默认处理方案
开始 异常名称 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 异常原因 Index 3 out of bounds for length 3 异常出现的位置 at com.itheima_01.ExceptionDmeo.method(ExceptionDmeo.java:13) 停止执行程序 at com.itheima_01.ExceptionDmeo.main(ExceptionDmeo.java:6)
package com.itheima_01;
public class ExceptionDmeo {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() {
int[] arr = {1, 2, 3};
// System.out.println(arr[1]);
System.out.println(arr[3]);
}
}
/*
开始
异常名称 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
异常原因 at com.itheima_01.ExceptionDmeo.method(ExceptionDmeo.java:13)
异常出现的位置 at com.itheima_01.ExceptionDmeo.main(ExceptionDmeo.java:6)
停止执行程序
*/
1.3、异常处理之try...catch
try{
可能出现异常的代码;
}catch(异常类名 变量名){
异常的处理代码;
}
执行流程:
程序从try里面的代码开始执行
出现异常,会自动生成一个异常对象类,该异常对象将被提交给Java运行时系统
当Java运行时系统接收到异常对象时,会到catch中去找匹配的异常类,找到后进行异常处理
执行完毕之后,程序还可以继续往下执行。
package com.itheima_02;
public class ExceptionDemo_01 {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() {
try{
int [] arr={1,2,3};
System.out.println(arr[3]);//new ArrayIndexOutOfBoundsException();
}catch (ArrayIndexOutOfBoundsException e){
// System.out.println("你输入的数组索引不存在");
e.printStackTrace();//意义在于程序还可以继续向下执行
}
}
}
1.4、Throwable的成员方法
ArrayIndexOutOfBoundsException e
e相当于你在程序出现问题时创建了一个对象,e前面的相当于是和e相匹配的类型,虽然程序都可以用Exception但是那样不方便程序的运行
public printStackTrace()
e.printStackTrace();//获取异常的错误信息,意义在于程序还可以继续向下执行 public String getMessage() 返回此throwable的详细字符串 System.out.println(e.getMessage());//获取异常的原因 public void printStackTrace() System.out.println(e.toString());//获取了getMessage里面的内容,还有异常的类名
package com.itheima_03;
public class ExceptionDemo {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() {
try{
int [] arr={1,2,3};
System.out.println(arr[3]);//new ArrayIndexOutOfBoundsException();
}catch (ArrayIndexOutOfBoundsException e){
// System.out.println("你输入的数组索引不存在");
// e.printStackTrace();//获取异常的错误信息,意义在于程序还可以继续向下执行
// public String getMessage() 返回此throwable的详细字符串
System.out.println(e.getMessage());//获取异常的原因
System.out.println(e.toString());//获取了getMessage里面的内容,还有异常的类名
}
}
}
/*
* public class Throwable {
public String getMessage() {
return detailMessage;
}
}
*
* */
1.5、编译时异常和运行时异常的区别
编译时异常(受检异常):必须显示处理,否则程序就会发生错误,无法通过编译(处理了问题程序才能跑) 可能有问题try catch 处理一下
运行时异常(非受检异常):无需显示处理,也可以和编译时异常一样处理(RuntimeException的所有子类都是)
package com.itheima_04;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExceptionDemo {
public static void main(String[] args) {
// method();
method2();
}
//编译时异常 可能尤问题try catch 处理一下
public static void method2() {
try{
String s="2048-10-01";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-d");
Date d=sdf.parse(s);
System.out.println(d);
}catch (ParseException e){
e.printStackTrace();
}
}
//运行时异常,通常用try catch处理
public static void method() {
try{
int [] arr={1,2,3};
System.out.println(arr[3]);//new ArrayIndexOutOfBoundsException();
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
}
1.6、异常处理之throws
学习了try catch进行处理异常,但是不是所有的情况都有权限处理,有些时候可能出现的异常处理不了。这个时候Java提供了throws方案
格式:
throws 异常类名;
注意:这个格式是跟在方法的括号后面的
编译时异常必须进行处理:两种处理方式①try ...catch或者②throws
将来谁调用水处理
运行时异常可以不处理,出了问题,需要我们回来修改代码,
package com.itheima_04;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExceptionDemo {
public static void main(String[] args) {
// method();
method2();
/*
想要使用method()需要进行try...catch处理
try {
method3();
} catch (ParseException e) {
e.printStackTrace();
}*/
method3();
}
//编译时异常 ①可能尤问题try catch 处理一下
public static void method2() {
try{
String s="2048-10-01";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-d");
Date d=sdf.parse(s);
System.out.println(d);
}catch (ParseException e){
e.printStackTrace();
}
}
// 编译时异常 ②如果我们处理不了 可以用throws将异抛出去,谁调用方法谁处理
public static void method3() throws ParseException {
String s="2048-10-01";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-d");
Date d=sdf.parse(s);
System.out.println(d);
}
//运行时异常,通常用try catch处理
public static void method() {
try{
int [] arr={1,2,3};
System.out.println(arr[3]);//new ArrayIndexOutOfBoundsException();
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
}
1.7、自定义异常
ctrl+N 弹出搜索框
为什么学习自定义异常?
有一些库里面没有的类需要我们自己写,然后再去Exception继承,这样当我们在执行程序时,出现我们预期的异常,就会报错。
package com.itheima_05;
public class TeacherTest {
public static void main(String[] args) {
Teacher t=new Teacher();
int score=10;
try {
t.checkScore(score);
} catch (ScoreException e) {
e.printStackTrace();
}
}
}