千家信息网

JAVA异常处理方式是什么

发表于:2024-10-11 作者:千家信息网编辑
千家信息网最后更新 2024年10月11日,这篇文章主要讲解了"JAVA异常处理方式是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"JAVA异常处理方式是什么"吧!1:try-catch结构
千家信息网最后更新 2024年10月11日JAVA异常处理方式是什么

这篇文章主要讲解了"JAVA异常处理方式是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"JAVA异常处理方式是什么"吧!

1:try-catch

结构:

* 结构:
* try{
* 可能出现异常de代码段
* }catch(可能出现的异常){
* 解决办法
* }


顺序:

* try-catch的执行顺序:
* 1、执行try块中的代码块 如果出现异常
* 2、通过出现的异常去匹配 catch中声明的异常类型
* 3、如果匹配成功 执行catch中的代码块 如果匹配失败 jvm处理当前异常信息 (终止程序 输出异常信息)
* 4、继续执行剩下的代码


例子:

  1. public class Test04 {

  2. public static void main(String[] args) {

  3. try{

  4. int num = 1/0;//new ArithmeticException()

  5. System.out.println(num);

  6. }catch(InputMismatchException e){ //InputMismatchException e = new ArithmeticException();

  7. System.out.println("除数不能为0");

  8. }

  9. System.out.println("嘿嘿");

  10. }

  11. }


注意事项

PS:只能处理一种异常信息。


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 获取可能没有捕获的异常信息


例子

  1. public class Test05 {

  2. public static void main(String[] args) {

  3. Scanner input = new Scanner(System.in);

  4. try{

  5. System.out.println("请输入被除数---->");

  6. int num1 = input.nextInt();

  7. System.out.println("请输入除数---->");

  8. int num2 = input.nextInt();

  9. System.out.println(num1+"/"+num2+"="+(num1/num2));

  10. }catch(InputMismatchException e){//这个异常对象中没有维护异常的原因 所以通过getMessage获取不到异常信息 null值

  11. //e.printStackTrace();

  12. System.out.println(e.getMessage());

  13. System.out.println("用户输入有误");

  14. }catch(ArithmeticException e){//这个异常对象中维护异常的原因 所以通过getMessage可以获取到异常信息

  15. System.out.println(e.getMessage());//by zero

  16. System.out.println("除数不能为0");

  17. }catch(Exception e){//Exception e = new 可能出现的异常(); 父类变量指向了子类对象

  18. //多态

  19. System.out.println(e.getMessage());

  20. System.out.println("外星人把页面叼走了 请等待。。。");

  21. }

  22. }

  23. }


注意事项:

PS:
  1. 一般情况下我们都会在最后一个catch中加入Exception 获取可能没有捕获的异常信息

  2. 常见的异常的对象中的方法:

* 异常中常见的方法:
* 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去关闭连接资源


例子:

  1. public class Test06 {

  2. public static void main(String[] args) {

  3. Scanner input = null;

  4. try{

  5. input = new Scanner(System.in);

  6. System.out.println("请输入被除数---->");

  7. int num1 = input.nextInt();

  8. System.out.println("请输入除数---->");

  9. int num2 = input.nextInt();

  10. System.exit(0);//关闭虚拟机 0正常退出 非0 强制退出

  11. System.out.println(num1+"/"+num2+"="+(num1/num2));

  12. }catch(InputMismatchException e){

  13. System.out.println("用户输入有误");

  14. }catch(ArithmeticException e){

  15. System.out.println("除数不能为0");

  16. }catch(Exception e){

  17. System.out.println("外星人把页面叼走了 请等待。。。");

  18. }finally{

  19. System.out.println("我被执行了");

  20. //在这里关闭的

  21. input.close();

  22. }

  23. }

  24. }


注意事项:

PS:
  1. finally一定会被执行 return 以及异常或者是正常情况下都会执行finally代码

  2. System.exit(数字) 退出虚拟机 0 正常 非0 强制



4:throws 声明一个异常

语法格式:

* 注意格式:
* 方法() throws 异常类型1,异常类型2。。。{}


注意事项:

  1. s不要忘记 一个方法可以声明多个异常信息

  2. 某个方法如果对外声明一个异常,那么调用者一定要解决当前异常。解决方案:

A、try-catch B、继续向外声明


案例:

  1. public class Test08 {

  2. public static void main(String[] args)throws Exception {

  3. /*try{

  4. //1、调用add方法

  5. add(1,2);

  6. }catch(Exception e){

  7. }*/

  8. System.out.println(add(1,43));

  9. }

  10. /*

  11. * 计算两个数相加

  12. * 调用这个方法可能出现异常

  13. * 这个方法就必须对外声明一个异常

  14. */

  15. public static int add(int num1,int num2)throws Exception{

  16. return num1+num2;

  17. }

  18. }


5:throw抛出异常信息

语法格式:

throw new 异常类型();
PS:抛出异常是在方法内部编写的


注意事项:

  1. throw 抛出异常在方法体体编写

  2. 一般情况下和throws一起使用


案例:

  1. public class Test09 {

  2. public static void main(String[] args) {

  3. //1、创建一个user对象

  4. User u = new User();

  5. //2、解决异常

  6. try {

  7. u.setGender(12);//new Exception();

  8. } catch (Exception e) {

  9. e.printStackTrace();

  10. }

  11. }

  12. }

  13. class User{

  14. private int gender;

  15. public User() {

  16. // TODO Auto-generated constructor stub

  17. }

  18. public int getGender() {

  19. return gender;

  20. }

  21. public void setGender(int gender) throws Exception{

  22. //判定gender的值

  23. if(gender==0||gender==1){

  24. this.gender = gender;

  25. }else{

  26. //抛出一个异常

  27. throw new Exception();

  28. }

  29. }

  30. }

6:自定义异常:

自定义异常的步骤:

* 如何自定义异常:
* 1、创建一个类 让当前类要么继承Exception 要么继承RuntimeException
* 2、编写当前类的构造器 :
* a、一定要写空构造器
* b、一定要写一个带异常原因描述的构造器 (带一个String参数的构造器)
* 3、在构造器内部通过super()调用父类的构造器即可


自定义异常如何获取异常信息:类图:

实例:

  1. public class GenderException extends Exception{

  2. public GenderException(){

  3. }

  4. public GenderException(String str){

  5. super(str);

  6. }

  7. }


测试类:

  1. public class Test11 {

  2. public static void main(String[] args) {

  3. //1、创建一个Person对象

  4. Person p = new Person();

  5. try{

  6. p.setGender(10);

  7. }catch(GenderException e){

  8. System.out.println(e.getMessage());

  9. }

  10. System.out.println(p.getGender()==0?"女生":"男生");

  11. }

  12. }

  13. class Person{

  14. private int gender;

  15. public Person() {

  16. // TODO Auto-generated constructor stub

  17. }

  18. public int getGender() {

  19. return gender;

  20. }

  21. public void setGender(int gender) throws GenderException,NullPointerException{

  22. if(gender==0||gender==1){

  23. this.gender = gender;

  24. }else{

  25. //抛出异常

  26. throw new GenderException("性别赋值错误");

  27. }

  28. }

  29. }


PS:当int作为属性时它是具有默认值,默认值是0.而这个值有可能导致程序运行期间出现不稳定因素

感谢各位的阅读,以上就是"JAVA异常处理方式是什么"的内容了,经过本文的学习后,相信大家对JAVA异常处理方式是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0