这篇文章主要讲解了“JAVA异常处理方式是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“JAVA异常处理方式是什么”吧!
1:try-catch
结构:
* 结构: * try{ * 可能出现异常de代码段 * }catch(可能出现的异常){ * 解决办法 * } |
顺序:
* try-catch的执行顺序: * 1、执行try块中的代码块 如果出现异常 * 2、通过出现的异常去匹配 catch中声明的异常类型 * 3、如果匹配成功 执行catch中的代码块 如果匹配失败 jvm处理当前异常信息 (终止程序 输出异常信息) * 4、继续执行剩下的代码 |
例子:
public class Test04 {
public static void main(String[] args) {
try{
int num = 1/0;//new ArithmeticException()
System.out.println(num);
}catch(InputMismatchException e){ //InputMismatchException e = new ArithmeticException();
System.out.println("除数不能为0");
}
System.out.println("嘿嘿");
}
}
|
注意事项
2:try-多重catch
结构:
* 结构: * try{ * 可能出现异常的代码 * }catch(异常类型1 e1){ * 解决方案1 * }catch(异常类型2 e2){ * 解决方案2 * }catch(异常类型3 e3){ * 解决方案3 * }。。。。{ * } |
顺序
* 执行顺序: * 1、执行try块 如果出现异常 * 2、以此匹配多重catch中声明的异常 * 3、如果匹配成功 执行当前匹配成功的catch块 try-catch块执行完毕 继续执行下面的代码 * 4、如果匹配失败 交由jvm处理 程序终止 输出异常信息 * 5、一般情况下我们都会在最后一个catch中加入Exception 获取可能没有捕获的异常信息 |
例子
public class Test05 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try{
System.out.println("请输入被除数---->");
int num1 = input.nextInt();
System.out.println("请输入除数---->");
int num2 = input.nextInt();
System.out.println(num1+"/"+num2+"="+(num1/num2));
}catch(InputMismatchException e){//这个异常对象中没有维护异常的原因 所以通过getMessage获取不到异常信息 null值
//e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("用户输入有误");
}catch(ArithmeticException e){//这个异常对象中维护异常的原因 所以通过getMessage可以获取到异常信息
System.out.println(e.getMessage());//by zero
System.out.println("除数不能为0");
}catch(Exception e){//Exception e = new 可能出现的异常(); 父类变量指向了子类对象
//多态
System.out.println(e.getMessage());
System.out.println("外星人把页面叼走了 请等待。。。");
}
}
}
|
注意事项:
PS:
一般情况下我们都会在最后一个catch中加入Exception 获取可能没有捕获的异常信息
常见的异常的对象中的方法:
* 异常中常见的方法: * e.getMessage() -->获取异常的原因藐视 * e.printStackTrace() -->打印异常的出现行数以及异常的全限定名* e.toString --> 异常的全限定名 |
3:try-多重catch-finally
结构:
* 结构: * try{ * 可能出现异常的代码 * }catch(异常类型1 e1){ * 解决方案1 * }catch(异常类型2 e2){ * 解决方案2 * }catch(异常类型3 e3){ * 解决方案3 * }。。。。{ * }finally{ * 代码块 * } |
顺序:
* 执行顺序: * 1、执行try块 如果出现异常 * 2、以此匹配多重catch中声明的异常 * 3、如果匹配成功 执行当前匹配成功的catch块 执行finally代码块 try-catch-finally块执行完毕 继续执行下面的代码 * 4、如果匹配失败 交由jvm处理 程序终止 输出异常信息 也会执行finally代码块 * 5、一般情况下我们都会在最后一个catch中加入Exception 获取可能没有捕获的异常信息 * 6、一般情况下通过finally去关闭连接资源 |
例子:
public class Test06 {
public static void main(String[] args) {
Scanner input = null;
try{
input = new Scanner(System.in);
System.out.println("请输入被除数---->");
int num1 = input.nextInt();
System.out.println("请输入除数---->");
int num2 = input.nextInt();
System.exit(0);//关闭虚拟机 0正常退出 非0 强制退出
System.out.println(num1+"/"+num2+"="+(num1/num2));
}catch(InputMismatchException e){
System.out.println("用户输入有误");
}catch(ArithmeticException e){
System.out.println("除数不能为0");
}catch(Exception e){
System.out.println("外星人把页面叼走了 请等待。。。");
}finally{
System.out.println("我被执行了");
//在这里关闭的
input.close();
}
}
}
|
注意事项:
PS: |
finally一定会被执行 return 以及异常或者是正常情况下都会执行finally代码
System.exit(数字) 退出虚拟机 0 正常 非0 强制
|
4:throws 声明一个异常
语法格式:
* 注意格式: * 方法() throws 异常类型1,异常类型2。。。{} |
注意事项:
s不要忘记 一个方法可以声明多个异常信息
某个方法如果对外声明一个异常,那么调用者一定要解决当前异常。解决方案:
A、try-catch B、继续向外声明 |
案例:
public class Test08 {
public static void main(String[] args)throws Exception {
/*try{
//1、调用add方法
add(1,2);
}catch(Exception e){
}*/
System.out.println(add(1,43));
}
/*
* 计算两个数相加
* 调用这个方法可能出现异常
* 这个方法就必须对外声明一个异常
*/
public static int add(int num1,int num2)throws Exception{
return num1+num2;
}
}
|
5:throw抛出异常信息
语法格式:
throw new 异常类型(); PS:抛出异常是在方法内部编写的 |
注意事项:
throw 抛出异常在方法体体编写
一般情况下和throws一起使用
|
案例:
public class Test09 {
public static void main(String[] args) {
//1、创建一个user对象
User u = new User();
//2、解决异常
try {
u.setGender(12);//new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class User{
private int gender;
public User() {
// TODO Auto-generated constructor stub
}
public int getGender() {
return gender;
}
public void setGender(int gender) throws Exception{
//判定gender的值
if(gender==0||gender==1){
this.gender = gender;
}else{
//抛出一个异常
throw new Exception();
}
}
}
|
6:自定义异常:
自定义异常的步骤:
* 如何自定义异常: * 1、创建一个类 让当前类要么继承Exception 要么继承RuntimeException * 2、编写当前类的构造器 : * a、一定要写空构造器 * b、一定要写一个带异常原因描述的构造器 (带一个String参数的构造器) * 3、在构造器内部通过super()调用父类的构造器即可 |
自定义异常如何获取异常信息:类图:
实例:
public class GenderException extends Exception{
public GenderException(){
}
public GenderException(String str){
super(str);
}
}
|
测试类:
public class Test11 {
public static void main(String[] args) {
//1、创建一个Person对象
Person p = new Person();
try{
p.setGender(10);
}catch(GenderException e){
System.out.println(e.getMessage());
}
System.out.println(p.getGender()==0?"女生":"男生");
}
}
class Person{
private int gender;
public Person() {
// TODO Auto-generated constructor stub
}
public int getGender() {
return gender;
}
public void setGender(int gender) throws GenderException,NullPointerException{
if(gender==0||gender==1){
this.gender = gender;
}else{
//抛出异常
throw new GenderException("性别赋值错误");
}
}
}
|
PS:当int作为属性时它是具有默认值,默认值是0.而这个值有可能导致程序运行期间出现不稳定因素
感谢各位的阅读,以上就是“JAVA异常处理方式是什么”的内容了,经过本文的学习后,相信大家对JAVA异常处理方式是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!