1.异常
error:不可处理
exception:可处理
(1)Runtime exception编译的时候不报错
(2)非Runtime exception不能通过编译
jvmjava虚拟机对异常的默认处理方案,找到异常的原因和位置,输出到控制台,终止程序。
1.1 try catch
package demo05Exception;
/*
与jvm处理的区别,程序可以继续执行
*/
public class Demo01try_catch {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method(){
try{
int[]arr={1,3,5};
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("你访问的数组不存在");
e.printStackTrace();
}
}
}
1.2 throwable 的三个成员方法
getMessage
toString
printStactTrace
package demo05Exception;
/*
与jvm处理的区别,程序可以继续执行
throwable中的3个成员方法
*/
public class Demo01try_catch {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method(){
try{
int[]arr={1,3,5};
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("你访问的数组不存在");
System.out.println(e.getMessage());//Index 3 out of bounds for length 3
System.out.println(e.toString());//java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
e.printStackTrace();
}
}
}
1.3 编译时异常和运行时异常
package demo05Exception;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
编译时异常和运行时异常
*/
public class Demo02Exception {
public static void main(String[] args) {
//运行时异常
method1();
//编译时异常,必须处理
method2();
}
public static void method1(){
try{
int [] arr={1,2,3};
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
public static void method2(){
String s="2022-12-11";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdf.parse(s);
System.out.println(d);
}catch(ParseException p){
p.printStackTrace();
}
}
}
1.4 throws通常用来有效解决编译时异常
package demo05Exception;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo03throws {
public static void main(String[] args) throws ParseException {
//运行时异常
System.out.println("开始");
//method1();
//编译时异常
method2();
System.out.println("结束");
}
public static void method1()throws ArrayIndexOutOfBoundsException {
int[] arr = {1, 2, 3};
System.out.println(arr[2]);
}
public static void method2() throws ParseException {
String s="2011-12-11";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(s);
System.out.println(d);
}
}
1.5 自定义异常
package demo05Exception;
import java.util.Scanner;
public class Demo04TeacherTest {
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();
}
}
}
package demo05Exception;
public class ScoreException extends Exception{
public ScoreException (){};
public ScoreException(String s){
super(s);
};
}
package demo05Exception;
import java.util.Scanner;
public class Teacher {
public void checkScore(int a) throws ScoreException {
if(a<0||a>100){
throw new ScoreException("分数不在正常范围");
}else{
System.out.println("分数正常");
}
}
}