异常
异常概述
异常:异常就是程序出现了不正常的情况
异常体系:Throwable(Error,Exception(RuntimeException,非RuntimeException))
- Error:严重问题不需要处理。比如内存不够
- Exception:称为异常类,它表示程序本身可以处理的问题。
- RuntimeException:在编译期是不检查的,出现问题后,需要我们返回去修改代码。
- eg:public class StudentDemo {
public static void main(String[] args) {
method();
}
public static void method(){
int[] a={1,2,3};
System.out.println(a[3]);//
}
} - 非RuntimeException:编译期就必须处理,否则程序不能通过编译,就更不能正常运行。
JVM的默认处理方案
- 把异常的名称,异常原因以及异常出现的位置等信息输出再来控制台
- 程序停止运行
异常处理
- 格式:try{输入可能出现问题的代码块}catch(异常类名 变量){输出异常情况}
- eg:public class
StudentDemo {
public static void main(String[] args) {
method();
}
public static void method(){
try {
int[] a = {1, 2, 3};
System.out.println(a[3]);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
} } 输出:Index 3 out of bounds for length 3(索引3超出数组长度)
Throwable的成员方法
- public String getMessage()
- public String toString()
- public void pintStackTrance()
public class StudentDemo {
public static void main(String[] args) {
method();
}
public static void method(){
try {
int[] a = {1, 2, 3};
System.out.println(a[3]);
}catch (ArrayIndexOutOfBoundsException e){
// public String getMessage() 返回此throwable的详细消息字符串
// System.out.println(e.getMessage());
// 输出 Index 3 out of bounds for length 3
// public String toString() 返回此可抛出简短描述
// System.out.println(e.toString());
// 输出 java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
// public void printStackTrance() 把异常的错误信息输出在控制台上
e.printStackTrace();
// 输出 java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
// at com.itheima.StudentDemo.method(StudentDemo.java:10)
// at com.itheima.StudentDemo.main(StudentDemo.java:5)
}
}
}
编译时异常与运行时异常的区别
- Java中异常常被分为两大类:编译时异常(受检异常),运行时异常(非受检异常)。
- 所有RuntimeException类机器子类的实例被称为运行时异常,其他异常都是编译时异常。
- 编译时异常:必须显示处理,否则程序就会发生错误,无法通过编译。
- 运行时异常:无需显示处理,也可以和编译时异常一起处理。
import java.text.SimpleDateFormat;
import java.util.Date;
public class StudentDemo {
public static void main(String[] args) {
String s="2020-7-12 22-22-22";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date f = sdf.parse(s);//编译时异常:parse会报错
method();
}
public static void method(){
int[] a = {1, 2, 3};
System.out.println(a[3]);//运行时异常;运行结果Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
// at com.itheima.StudentDemo.method(StudentDemo.java:14)
// at com.itheima.StudentDemo.main(StudentDemo.java:10)
}
}
throws
- 格式:throws 类名;***这个格式是跟在方法的括号后面的
- 编译时异常必须要进行处理:try{}catch{}或者throws,如果采用throws这种方案,将来谁调用谁处理。
- 运行时异常可以不处理,出问题后,需要我们回来修改代码。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StudentDemo {
public static void main(String[] args){
try {
method1();
} catch (ParseException e) {
e.printStackTrace();
}
method2();
}
public static void method1() throws ParseException {
String s="2020-7-12 22-22-22";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date f = sdf.parse(s);
System.out.println(f);
}
public static void method2() throws ArrayIndexOutOfBoundsException{
int[] i={1,2,3};
System.out.println(i[3]);
}
}
自定义异常
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.cheakScore(score);
} catch (ScoreException e) {
e.printStackTrace();
}
}
}
public class Teacher {
public void cheakScore(int score) throws ScoreException{
if(score<0||score>100){
throw new ScoreException("你报的分数有误,不在0-100之间");
}else {
System.out.println("分数正常");
}
}
}
public class ScoreException extends Exception{
public ScoreException(){}
public ScoreException(String message){
super(message);
}
}
throws和throw的区别
| throws |throw |
|用在方法声明后的异常类名|用在方法体内异常对象名|
| 表示抛出异常,由该方法的调用者来处理|表示抛出异常,由方法体内的语句处理|
| 表现出异常的一种可能性,并不一定会发生这些异常 | 执行throw一定抛出了某种异常 |