异常处理
1.什么是异常
异常是阻止当前方法或作用域继续执行的问题,在程序中导致程序中断运行的一些指令.
2.try与catch关键字
在程序中出现异常,就必须进行处理,处理格式如下:
try(…){
//JDK1.7以后直接释放资源
//有可能发生异常的代码段
}catch(异常类型对象){
//异常的处理操作
}catch(异常类型对象){
//异常的处理操作
}…
finally{
//异常的统一出口
}
3.除数为零的异常
Exception in thread “main” java.lang.ArithmeticException: / by zero
ArithmeticException:算术异常
4.插入Exception出现错误
No exception of type Exception can be thrown; an exception type must be a subclass of Throwable Exception.java
翻译:不能引发异常类型的异常;异常类型必须是throwable exception.java的字类.
解决方法
5.异常处理过程分析
一旦产生异常,则系统会自动产生一个异常类的实例化对象
此时如果存在了try语句,则会自动找到匹配的catch语句执行,如果没有异常处理,则程序将退出,并由系统报告错误.
所有的catch根据方法的参数匹配异常的实例化对象,如果匹配成功,则表示由此catch进行处理
6.ArrayIndexOutOfBoundsException异常处理
数组下表越界异常处理
7.NullPointerException异常处理
空指针异常处理
8.finally关键字
在进行异常的处理之后,在异常的处理格式中还有一个finally语句,那么此语句将作为异常的统一出口,不管是否产生了异常,最终都要执行此段代码.
9.throw与throws关键字
throws关键字主要在方法的声明上使用,表示方法中不处理异常,而交给调用处处理.实际上对于Java程序来讲,如果没有加入任何的异常处理,默认由JVM进行异常的处理操作.
throw关键字表示在程序中手动抛出一个异常,因为从异常处理机制来看,所有的异常一旦产生后,实际上抛出的就是一个异常类的实例化对象,那么此对象也可以有throw直接抛出.
10.异常处理的语法规则
try语句不能单独存在,可以和catch,finally组成try…catch…finally,try…catch,try…finally三种结构,catch语句可以有一个或多个,finally语句最多一个,try,catch,finally这三个关键字均不能单独使用.
try,catch,finally三个代码块中变量的作用域分别独立而不能相互访问.
多个catch块时候,Java虚拟机会匹配其中一个异常类或其子类,就执行这个catch块,而不会再执行别的catch块.
11.自定义异常
在Java中,已经提供了很多的异常类的定义,但是我们在实际项目开发中,可能需要使用一些自己的异常类,那么可以通过继承Exception类或已有的异常类的方式完成一个自定义异常类的操作.
ClassNotFoundException
DataFormatException
RuntimeException
ArithmeticException
IndexOutOfBoundException
NullPointerException
ClassCastException
…
12.Unhandled exception type MyException
翻译:未处理的异常类型MyException
13.受检与非受检异常
受检异常:Exception
定义方法时必须声明所有可能会抛出的exception;在调用这个方法时,必须捕获它的checked exception,不然就得把它exception传递下去;exception是从java.lang.Exception类衍生出来的.例如:IOException,SQLException就属于Exception
非受检异常:RuntimeException
在定义方法时不需要声明会抛出runtime exception;在调用这个方法时不需要捕获这个runtime exception;runtime exception是从java.lang.RuntimeException或java.lang.Error类衍生出来的.例如:NullException,IndexOutOfBoundException就属于runtime exception.
注意
受检异常是编译时异常,必须用try catch,非受检异常是运行时异常,可以不用显示使用try catch
14.assert关键字
表示断言
当这个程序执行到某个固定位置的时候,在程序中的某个变量的取值肯定是预期的结果,那么这种操作可以使用断言完成.
断言的操作语法:
assert 表达式;
注意:assert断言通常在调试程序时使用,运行程序时需要添加VM参数-ea,断言才能起效果.
设置过程:
项目测试代码
Exception1.java
package com.dream.Ex;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* 2022年2月26日
* @company
* @author dream
* @description 异常处理
* 1.Throwable是异常的基类,分为Error和Exception,在编程中我们关注Exception
* 2.Exception分为编译器异常(受检)和运行期异常(非受检)
* 3.异常会导致程序中断,无法继续执行
* 4.在开发中,我们需要把可能出现异常的代码使用try语句块包裹起来
* 5.处理异常可以让程序保持运行状态
* 6.catch可以有多个,顺序为从子类到父类,大的放后面,小的放前面
* 7.当代码出现异常,会产生异常对象(JDK代码或JVM),把异常对象传入被匹配的catch中异常对象变量.
* 8.throw在方法内使用,throws在方法声明中使用
*
* 常用异常:
* ArithmeticException算数异常
* ArrayIndexOutOfBoundException数组下标越界异常
* NullPointerException空指针异常
* InputMismatchException输入匹配异常
* Exception异常父类
* RuntimeException运行时间异常
* ClassNotFoundException类未找到异常
* DataFormatException数据格式化异常
* ClassCastException类型转换异常
*
*/
public class Exception1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// div(10, 2);
// int c=method();
// System.out.println(c);
// div2(10, 0);
input1();
}
/**
* 除法运算
*/
private static void div(int num1,int num2){
int arr[]= {1,2,3,4,5};
try {
int result=num1/num2;
//alt+/
System.out.println("result="+result);
System.out.println(arr[5]);
System.out.println(arr.length);
}catch (ArithmeticException e) {
System.out.println("除数不能为0!");
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界!");
}catch(NullPointerException e) {
System.out.println("空指针异常!");
}catch(Exception e) {
System.out.println("数据出现异常错误!");
}finally {//最终语句
System.out.println("try异常处理程序执行完毕!");
}
System.out.println("程序结束!");
}
public static int method() {
int a=10;
int b=0;
int c=0;
try {
System.out.println("a="+a);
System.out.println("b="+b);
c=a/b;
System.out.println("a/b="+c);
return c;
}catch(Exception e) {
//代码测试时使用,打印错误栈的内容
e.printStackTrace();
}finally {
System.out.println("finally");
}
return -1;
}
//自动补全:alt+/
private static int div2(int a,int b)throws ArithmeticException{
// int c=a/b;
// return c;
try {
int c=a/b;
return c;
}catch (ArithmeticException e) {
throw new ArithmeticException("除数不能为零!");
}finally {
System.out.println("运行结束");
}
}
public static void input1() {
Scanner inputScanner=new Scanner(System.in);
try {
int num=inputScanner.nextInt();
System.out.println(num);
}catch(InputMismatchException e) {
System.out.println("输入不匹配!");
}
}
}
MyException.java
package com.dream.Ex;
/**
*
* 2022年2月26日
* @company
* 自定义异常类的实现是,提供构造方法
* 异常对象本身是没有实际功能,只是一个有意义的标识
* @author dream
* @description
* 自定义异常通常都是通过继承一个异常类来实现
* 1.Throwable
* 2.Exception
* 3.RuntimeException
*
*/
//也可以使用Exception受检异常的检查
public class MyException extends RuntimeException{
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
}
UserService.java
package com.dream.Ex;
/**
*
* 2022年2月26日
* @company
* @author dream
* @description
* 用户服务判定
*/
public class UserService {
public UserService() {
// TODO Auto-generated constructor stub
}
public User login(String name,String password) throws MyException{
if(!"admin".equals(name)) {
throw new MyException("用户名错误!");
}
if(!"12345".equals(password)) {
throw new MyException("密码错误");
}
User user=new User("admin","12345",12,"男");
return user;
}
}
User.java
package com.dream.Ex;
public class User {
private String name;
private String password;
private int age;
private String sex;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String password, int age, String sex) {
super();
this.name = name;
this.password = password;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + ", age=" + age + ", sex=" + sex + "]";
}
}
LoginDemo.java
package com.dream.Ex;
import java.util.Scanner;
/**
*
* 2022年2月27日
* @company
* @author dream
* @description
* 面试题:Exception与RuntimeException的区别
* Exception:受检异常,在编程期检查,在调用抛出这个异常的方法时,必须显示的使用try..catch...
* RuntimeException:非受检异常,在运行时检查,在调用抛出这个异常的方法时,可以不用显示的使用try...catch..
* 在使用自定义异常时,根据实际的业务要求,来决定使用哪个作为父类(一般情况是使用RuntimeException)
*
*/
public class LoginDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("请输入用户名:");
String name=input.nextLine();
System.out.print("请输入密码:");
String password=input.nextLine();
UserService us=new UserService();
try {
User user=us.login(name, password);
System.out.println("登录成功!");
System.out.println(user);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
AssertDemo.java
package com.dream.Ex;
/**
*
* 2022年2月27日
* @company
* @author dream
* @description
*/
public class AssertDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int c=add(10,10);
assert c==10:"结果不正确!";
}
private static int add(int a,int b) {
return a+b;
}
}