Java学习 - 黑马(第二部分) - 随堂笔记 - 09_异常
1. 异常
1.1 异常概述
package com.itheima_01;
import java.lang.reflect.Method;
/*
异常
*/
public class ExceptionDemo01 {
public static void main(String[] args) {
method();
}
public static void method() {
int[] arr = {1, 2, 3};
System.out.println(arr[1]);//2
//System.out.println(arr[3]);//ArrayIndexOutOfBoundsException
//数组索引越界异常
System.out.println(arr[2]);
}
}
异常:就是程序出现了不正常的情况
异常体系
Error:严重问题,不需要处理
Exception:称为异常类,它表示程序本身可以处理的问题
- RuntimeException:在编译期是不检查的,出现问题后(运行后),需要我们回来修改代码
- 非RuntimeException:编译期就必须处理的,否则程序不能通过编译,就更不能正常运行了
1.2 JVM的默认处理方案
如果程序出现了问题,我们没有做任何处理,最终JVM会做默认的处理
- 把异常的名称,异常原因及异常出现的位置信息输出在了控制台
- 程序停止执行
package com.itheima_01;
import com.sun.jdi.Method;
import com.sun.jdi.PathSearchingVirtualMachine;
import java.sql.SQLOutput;
/*
JVM的默认处理方案
*/
public class ExceptionDemo02 {
public static void main(String[] args) {
System.out.println("开始");
methed();
System.out.println("结束");
}
public static void methed(){
int[] arr = {1,2,3};
System.out.println(arr[1]);
System.out.println(arr[3]);
}
}
/*
开始
2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at com.itheima_01.ExceptionDemo02.methed(ExceptionDemo02.java:20)
at com.itheima_01.ExceptionDemo02.main(ExceptionDemo02.java:14)
*/
1.3 异常处理
如果程序出现了问题,我们需要自己来处理,有两种方案
1.4 异常处理之try…catch…
try{
可能出现异常的代码;
}catch(异常类名 变量名){
异常的处理代码;
}
执行流程:
程序从try里面的代码开始执行
出现异常,会自动生成一个异常类对象,该异常对象将被提交给Java运行时系统
当Java运行时系统接收到异常对象时,会到catch中去找匹配的异常类,找到后进行异常的处理
执行完毕之后,程序还可以继续往下执行
package com.itheima_02;
/*
try{
可能出现异常的代码;
}catch(异常类名 变量名){
异常的处理代码;
}
*/
public class ExceptionDemo01 {
public static void main(String[] args) {
System.out.println("开始");
methed();
System.out.println("结束");
}
public static void methed(){
try {
int[] arr = {1, 2, 3};
System.out.println(arr[1]);
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException e){
//System.out.println("你访问的数组的索引不存在!");
e.printStackTrace();//也会报错,但是可以继续往下执行
}
}
}
Throwable的成员方法
package com.itheima_02;
/*
public String getMessage();返回此throwable的详细消息字符串
public String toString();返回此可抛出的简短描述
public void printStackTrace();把异常的错误信息输出在控制台
*/
public class ExceptionDemo02 {
public static void main(String[] args) {
System.out.println("开始");
methed();
System.out.println("结束");
}
public static void methed() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);//new ArrayIndexOutOfBoundsException(xxx)
} catch (ArrayIndexOutOfBoundsException e) {
//e.printStackTrace();
//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 printStackTrace();把异常的错误信息输出在控制台
e.printStackTrace();
}
}
}
/*
public class Throwable{
private String detailMessage;
public Throwable(String message){
detailMessage = message;
}
public String getMessage() {
return detailMessage;
}
}
*/
1.6 编译时异常和运行时异常的区别
Java中的异常被分为两大类:编译时异常和运行时异常,也被称为受检异常和非受检异常
所有的RuntimeException类及其子类被称为运行时异常,其他的异常都是编译时异常
- 编译时异常:必须显示处理,否则程序就会发生错误,无法通过编译
- 运行时一次:无需显示处理,也可以和编译时异常一样处理
package com.itheima_02;
import java.nio.channels.NonReadableChannelException;
import java.security.PublicKey;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
Java中的异常被分为两大类:**编译时异常**和**运行时异常**,也被称为**受检异常**和**非受检异常**
所有的RuntimeException类及其子类被称为运行时异常,其他的异常都是编译时异常
- 编译时异常:必须显示处理,否则程序就会发生错误,无法通过编译
- 运行时一次:无需显示处理,也可以和编译时异常一样处理
*/
public class ExceptionDemo03 {
public static void main(String[] args) {
//method();
method2();//Sun Aug 09 00:00:00 CST 2048
}
//编译时异常
public static void method2(){
try {
String s = "2048-08-09";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(s);
System.out.println(date);
}catch (ParseException e){
e.printStackTrace();
}
}
//运行时异常
public static void method(){
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);//ArrayIndexOutOfBoundsException
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
}
1.7 异常处理之throws
虽然我们通过try…catch…可以对异常进行处理,但是并不是所有的情况我们都有权限进行异常的处理
也就是说,有些时候可能出现异常是我们处理不了的,这个时候该怎么办呢?
针对这种情况,Java提供了throws的处理方案
格式:
throws 异常类名;
注意:这个格式是跟在方法的括号后面的
- 编译时异常必须要进行处理,两种处理方案:try…catch…或者throws,如果采用throws这种方案,将来谁调用谁处理
- 运行时异常可以不处理,出现问题后,需要我们回来修改代码
package com.itheima_03;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
throws 异常类名;
**注意**:这个格式是跟在方法的括号后面的
*/
public class ExceptionDemo {
public static void main(String[] args) {
System.out.println("开始");
//method();
System.out.println("+++++++++++");
try {
method2();//Sun Aug 09 00:00:00 CST 2048
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("结束");
}
//编译时异常
public static void method2() throws ParseException {
String s = "2048-08-09";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(s);
System.out.println(date);
}
//运行时异常
public static void method() throws ArrayIndexOutOfBoundsException{
int[] arr = {1, 2, 3};
System.out.println(arr[3]);//ArrayIndexOutOfBoundsException
}
}
1.8 自定义异常
格式:
public class 异常类名 extends Exception{
无参构造
带参构造
}
范例:
public class ScoreException extends Exception {
public ScoreException(){}
public ScoreException(String message){
super(message);
}
}
package com.itheima_04;
public class ScoreException extends Exception{
public ScoreException() {
}
public ScoreException(String message) {
super(message);
}
}
package com.itheima_04;
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("分数正常");
}
}
}
package com.itheima_04;
import java.util.Scanner;
public class TeacherTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入分数:");
int score = scanner.nextInt();
Teacher t = new Teacher() ;
try {
t.checkScore(score);
}catch (ScoreException e){
e.printStackTrace();
}
}
}
1.9 throws和throw的区别
throws
- 用在方法声明后面,跟的是异常类名
- 表示抛出异常,由该方法的调用者来处理
- 表示出现异常的一种可能性,并不一定会发生这些异常
throw
- 用在方法体内,跟的是异常对象名
- 表示抛出异常,由方法体内的语句处理
- 执行throw一定抛出了某种异常
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)