目录
一、异常概念
1、什么是异常
2、try与catch关键字
二、try_catch_finally
1、catch
2、异常处理过程分析
3、finally
三、throw_throws与异常规则
1、throw throws
2、异常处理的语法规则
四、自定义异常与assert
1、自定义异常
2、受检和非受检异常
i、受检异常:Exception
ii、非受检异常:RuntimeException
iii、面试题
3、assert
五、新特性与debug技术
1、新特性
2、debug
一、异常概念
1、什么是异常
异常时阻止当前方法或作用域继续执行的问题,在程序中导致程序中断运行的一些指令。
2、try与catch关键字
Throwable是异常的基类,分为Error和Exception,在编程中我们关注Exception;
Exception分为编译期异常(受检)和运行期异常(非受检)
异常会导致程序中断,无法继续执行
在开发中我们需要把可能出现异常的代码使用try语句块包裹起来
处理异常可以让程序保持运行状态
package yichang;
public class Test {
public static void main(String[] args) {
div(10,0);
}
//除法运算
private static void div(int num1,int num2){
try {
int result = num1 / num2;
System.out.println("result="+result);
}catch (ArithmeticException e){ //数学算数异常
System.out.println("除数不能为0");
}
System.out.println("程序运行结束");
}
}
二、try_catch_finally
1、catch
catch可以有多个异常,顺序为从子类到父类:
package yichang;
public class Test {
public static void main(String[] args) {
div(10,0);
}
//除法运算
private static void div(int num1,int num2){
int[] arr = {1,2,3,4,5};
try {
System.out.println(arr[4]); //数组下标越界
arr=null;
System.out.println(arr.length);
int result = num1 / num2;
System.out.println("result="+result);
}catch (ArithmeticException e){
System.out.println("除数不能为0");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数组下标越界");
}catch (NullPointerException e){
System.out.println("空指针异常");
}
System.out.println("程序运行结束");
}
}
catch异常的父类:
package yichang;
public class Test {
public static void main(String[] args) {
div(10,0);
}
//除法运算
private static void div(int num1,int num2){
int[] arr = {1,2,3,4,5};
try {
System.out.println(arr[4]); //数组下标越界
arr=null;
System.out.println(arr.length); //空指针
int result = num1 / num2;
System.out.println("result="+result);
}catch (Exception e){
System.out.println("出错了!异常类型为:"+e);
}
System.out.println("程序运行结束");
}
}
2、异常处理过程分析
一旦产生异常,则系统会自动产生一个异常类的实例化对象;
此时如果存在了try语句,则自动会找到匹配的catch语句执行,如果没有异常,则程序退出,系统报告错误;
所有的catch根据方法的参数匹配异常类的实例化对象,如果匹配成功,侧表示由此catch进行处理;
3、finally
表示有没有异常都会执行的代码块
/**
* 面试题:final会不会执行 会
*
*/
package yichang;
public class Test {
public static void main(String[] args) {
method();
}
//除法运算
private static int method(){
int a=10;
int b=5;
try{
System.out.println("a="+a);
System.out.println("b="+b);
int c = a/b;
System.out.println("a/b="+c);
return c;
}catch (Exception e){
//代码测试时使用
e.printStackTrace();//打印栈信息
}finally {
System.out.println("finally");
}
return -1;
}
}
三、throw_throws与异常规则
1、throw throws
throws关键字主要在方法的声明上使用,表示方法中不处理异常,而交给调用处处理。实际上对于Java程序来讲,如果没有加入任何的异常处理,默认有JVM进行异常的处理操作。
throw关键字表示在程序中手动抛出一个异常,因为从异常处理机制来看,所有的异常一旦产生之后,实际上抛出的就是一个异常类的实例化对象,那么此对象也可以有throw直接抛出。
两者一般在一起用
package yichang;
public class Test1 {
public static void main(String[] args) {
div2(10,0);
}
private static int div2(int a,int b) throws ArithmeticException{
try{
int c=a/b;
return c;
}catch (ArithmeticException e){
throw new ArithmeticException("除数不能为0");
}finally {
System.out.println("运行结束");
}
}
}
throw在方法内使用,throws在方法声明中使用
2、异常处理的语法规则
i、try语句不能单独存在,可以和catch、finally组成try...catch...finally、try...catch、try...finally三种结构,catch语句可以有一个或多个,finally语句最多一个,try、catch、finally这三个关键字均不能单独使用。
ii、try、catch、finally三个代码块中变量的作用域分别独立而不能相互访问。、
iii、多个catch块时候,java虚拟机会匹配其中一个异常类或其子类,就执行这个catch块,而不会在执行别的catch块。
package yichang;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
input();
}
private static void input(){
Scanner input = new Scanner(System.in);
try {
int i = input.nextInt();
System.out.println(i);
}catch (InputMismatchException e){
System.out.println("输入不匹配");
}
}
}
四、自定义异常与assert
在Java中,已经提供了很多异常类的定义,但是我们实际开发中,可能需要一些自己的异常类,那么可以通过继承Exception类或已有的异常类的方式完成一个自定义异常类的操作。
自定义异常通常都是通过继承一个异常类来实现
Throwable、Exception、RuntimeException
自定义异常通常的实现是,重写父类的构造方法;异常对象本身没有实际功能,只是一个有意义的标识。
1、自定义异常
异常类
package yichang;
public class MyException extends Exception {
public MyException(){
super();
}
public MyException(String message){
super(message);
}
}
用户类
package yichang;
public class User {
private String name;
private String password;
private int age;
private String sex;
public User() {
super();
}
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 + '\'' +
'}';
}
}
用户使用
package yichang;
public class UserService {
public User login(String username,String password)throws MyException {
if(!"admin".equals(username)){
throw new MyException("用户名错误");
}
if(!"12345".equals(password)){
throw new MyException("密码错误");
}
User user = new User("admin","12345",18,"男");
return user;
}
}
测试demo
package yichang;
import java.util.Scanner;
public class LonginDemo {
public static void main(String[] args) {
System.out.println("请输入用户名");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
System.out.println("请输入密码");
String pass = input.nextLine();
UserService us = new UserService();
try {
User user = us.login(name,pass);
System.out.println("登录成功");
System.out.println(user);
} catch (MyException e) {
e.printStackTrace();
}
}
}
2、受检和非受检异常
i、受检异常:Exception
定义方法时必须声明所有可能会抛出的exception;在调用这个方法时,必须捕获它的checked exception,不然就得把它的exception传递下去;exception是从java.lang.Exception类衍生出来的。例如:IOException,SQLException就属于Exception;
ii、非受检异常:RuntimeException
在定义方法时不需要声明会抛出runtime exception;在调用这个方法时不需要捕获这个runtime exception;runtime exception是从java.lang.RuntimeException或者java.lang.Error类衍生出来的。例如:NullPointException,IndexOutOfBoundsException就属于runtime exception;
iii、面试题
Exception与RuntimeException区别
Exception:受检异常,在编译器检查,在调用抛出这个异常的方法时,必须显示的使用try...catch...
RuntimeException:非受检异常,在运行期检查,在调用抛出异常的方法时,可以不用显示的使用try...catch...
在定义异常时,根据实际业务选择使用
3、assert
assert关键字,表示断言
当程序执行到某个固定位置的时候,程序中的某个变量的取值肯定是预期的结果,那么这种操作可以使用断言完成。
语法:
assert 表达式;
package yichang;
public class AssertDemo {
public static void main(String[] args) {
int result = add(10,10);
assert result ==10:"结果不正确";
}
private static int add(int a,int b){
return a+b;
}
}
五、新特性与debug技术
1、新特性
jdk1.5后出现Scanner输入,1.5之前接受控制台输入,直接使用IO流,通过BufferedReader
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String info = br.readLine();
//br.close()
}catch(IOException|NumberFormatException e){//简介catch,但是要写并列的异常
e.printStackTrace();
}
这种使用就可以自动关闭流,不需要手动关闭
2、debug
调试时程序员编码过程中找逻辑错误的一个很重要的手段;
断点:遇到断点,暂挂,等候命令
debug as --> Java Application
Rerun 'xxxx':重新运行程序,会关闭服务后重新启动程序。
Update 'tech' application (Ctrl + F5):更新程序,一般在你的代码有改动后可执行这个功能。而这个功能对应的操作则是在服务配置里,如图2.3。
Resume Program (F9):恢复程序,比如,你在第20行和25行有两个断点,当前运行至第20行,按F9,则运行到下一个断点(即第25行),再按F9,则运行完整个流程,因为后面已经没有断点了。
Pause Program:暂停程序,启用Debug。目前没发现具体用法。
Stop 'xxx' (Ctrl + F2):连续按两下,关闭程序。有时候你会发现关闭服务再启动时,报端口被占用,这是因为没完全关闭服务的原因,你就需要查杀所有JVM进程了。
View Breakpoints (Ctrl + Shift + F8):查看所有断点,后面章节会涉及到。
Mute Breakpoints:哑的断点,选择这个后,所有断点变为灰色,断点失效,按F9则可以直接运行完程序。再次点击,断点变为红色,有效。如果只想使某一个断点失效,可以在断点上右键取消
注意:需要debug模式启动项目