异常概述
JVM的默认处理方案
异常处理 try…catch…
作用:使程序在处理异常的同时,也可以往下执行
package heima;
public class P215 {
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]);
}catch(ArrayIndexOutOfBoundsException e){
// System.out.println("访问的数组的索引不存在");
e.printStackTrace();
}
}
}
Throwable成员方法
三种方法的演示,输出在//后面:
package heima;
public class P216 {
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]);
}catch (ArrayIndexOutOfBoundsException e){
// e.printStackTrace();
//getMessage() 返回此throwable的详细消息字符串。
// System.out.println(e.getMessage()); //3
//toString() 返回此可抛出的简短描述。
// System.out.println(e.toString());//java.lang.ArrayIndexOutOfBoundsException: 3
}
}
}
编译时异常和运行时异常
- 运行时异常
package heima;
public class P217 {
public static void main(String[] args) {
method();
}
//运行时异常
public static void method() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) { //因为该异常类的父类是RuntimeException
// e.printStackTrace();
}
}
}
原因:该异常类的父类是RuntimeException
- 编译时异常
编译阶段就会报错:
鼠标停在上方,发现问题:
同样进行抛出处理,编译正常:
package heima;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class P217 {
public static void main(String[] args) {
method2();
}
//编译时异常
public static void method2(){
try {
String s = "2022-11-13";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy--MM--dd");
Date d = sdf.parse(s);
System.out.println(d);
}catch (ParseException e){
e.printStackTrace();
}
}
}
异常处理 throws
- 对运行时异常 --没有效果
public static void main(String[] args) {
System.out.println("开始");
method1();
System.out.println("结束");
}
public static void method1() throws ArrayIndexOutOfBoundsException{
int[] arr = {1,2,3};
System.out.println(arr[3]);
}
结果同没抛出前
- 对编译时异常 --想继续执行,依然要使用try-catch
只不过使用throws,可以抛给调用者处理
package heima;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class P217 {
public static void main(String[] args) {
System.out.println("开始");
try {
method3();
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("结束");
}
//编译时异常
public static void method2(){
try {
String s = "2022-11-13";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy--MM--dd");
Date d = sdf.parse(s);
System.out.println(d);
}catch (ParseException e){
e.printStackTrace();
}
}
public static void method3() throws ParseException{
String s = "2022-11-13";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy--MM--dd");
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]);
} catch (ArrayIndexOutOfBoundsException e) { //因为该异常类的父类是RuntimeException
// e.printStackTrace();
}
}
//运行时异常 throws
public static void method1() throws ArrayIndexOutOfBoundsException{
int[] arr = {1,2,3};
System.out.println(arr[3]);
}
}
自定义异常
- 无参构造
ScoreException.java
package heima.P219;
public class ScoreException extends Exception {
public ScoreException() {}
public ScoreException(String message){
super(message);
}
}
Teacher.java
package heima.P219;
public class Teacher {
public void checkScore(int score) throws ScoreException{
if(score<0 || score>100){
throw new ScoreException();
}else {
System.out.println("分数正常");
}
}
}
TeacherTest.java
package heima.P219;
import java.util.Scanner;
public class TeacherTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入分数:");
int score = sc.nextInt();
Teacher t = new Teacher();
try {
t.checkScore(score);
}catch (ScoreException e){
e.printStackTrace();
}
}
}
输出:
- 自定义提示信息 --有参构造
改动Teacher.java
package heima.P219;
import javax.script.ScriptException;
public class Teacher {
public void checkScore(int score) throws ScoreException{
if(score<0 || score>100){
// throw new ScoreException();
throw new ScoreException("你给的分数有误,分数应该在0~100之间");
}else {
System.out.println("分数正常");
}
}
}