java基础学习之异常Exception 十

柔情只为你懂 2024-02-17 19:38 153阅读 0赞

异常Exception

  1. package exception;
  2. import java.io.FileInputStream;
  3. /**
  4. * 异常 : 程序出现了不正常的情况
  5. * @author Angus
  6. *
  7. * 我们在编写程序的时候,可能有很多问题存在,为了将来方便的表示浙西额的问题原因,类型,位置,java就提供了异常对象供我们使用
  8. */
  9. public class ExceptionDemo {
  10. public static void main(String[] args) {
  11. int a = 10;
  12. int b = 0;
  13. System.out.println(a / b);
  14. //Exception in thread "main" java.lang.ArithmeticException: / by zero
  15. // FileInputStream fis = new FileInputStream("a.txt");
  16. }
  17. }

类 Throwable

Throwable是java.lang包中一个专门用来处理异常的类。它有两个子类,即Error和Exception,它们分别用来处理两组异常

  1. Throwable
  2. * Error:严重问题, 需要修改代码或者设备支持。。
  3. * Exception:不严重
  4. * 编译期间:需要我们自己处理
  5. * 运行期间:不需要处理,需要修改代码或者传递参数
  6. package exception;
  7. import java.io.FileInputStream;
  8. import java.util.Scanner;
  9. /**
  10. * @author Angus
  11. * 出现怎么解决呢?
  12. *
  13. * 如果我们没有处理,java虚拟机在会控制台打印错误。。
  14. *
  15. * A基本格式:
  16. * try{
  17. * 可能发生问题的代码
  18. * }catch(异常类名 变量名){
  19. * 异常处理
  20. * }
  21. * B 抛出
  22. *
  23. */
  24. public class ExceptionDemo {
  25. public static void main(String[] args) {
  26. Scanner sc = new Scanner(System.in);
  27. System.out.println("请输入数据");
  28. int a = sc.nextInt();
  29. int b = sc.nextInt();
  30. try{
  31. int c = a/b ;
  32. System.out.println(c);
  33. }catch(Exception e){
  34. System.out.println(e); //异常输出
  35. }
  36. }
  37. }

Throwable方法使用

  1. package exception;
  2. import java.io.FileInputStream;
  3. import java.util.Scanner;
  4. /**
  5. * @author Angus
  6. * Throwable
  7. *
  8. * getMessage() 返回此 throwable 的详细消息字符串。
  9. * toString() 返回此 throwable 的简短描述。
  10. * printStackTrace() 将此 throwable 及其追踪输出至标准错误流
  11. */
  12. public class ExceptionDemo {
  13. public static void main(String[] args) {
  14. int a = 10;
  15. int b = 0;
  16. try{
  17. System.out.println(a /b );
  18. }catch(Exception e){
  19. // System.out.println(e);
  20. System.out.println(e.getMessage()); // / by zero
  21. System.out.println(e.toString()); // java.lang.ArithmeticException: / by zero
  22. e.printStackTrace();
  23. }
  24. }
  25. }

finally简单使用:

  1. package exception;
  2. /**
  3. *
  4. * @author Angus
  5. * 基本格式
  6. * try{
  7. * 有问题代码
  8. * }catch{
  9. * 处理方案
  10. * }finally{
  11. * 永远执行。。。
  12. * }
  13. */
  14. public class FinallyDemo {
  15. public static void main(String[] args) {
  16. int a = 10;
  17. int b = 0;
  18. try{
  19. System.out.println(a /b);
  20. }catch(Exception e){
  21. e.printStackTrace();
  22. }finally{
  23. System.out.println("over");
  24. }
  25. }
  26. }

结果:

Center

测试:常见面试题

  1. package exception;
  2. /**
  3. *
  4. * @author Angus
  5. *
  6. * 1;final,fianlly,finalize 区别?
  7. * 2:finally会永远执行吗?
  8. * 会,但是有一个特殊情况,在执行到finally之前JVM退出。。。
  9. * 3:在catch中加入return finally代码是否执行?
  10. * 会执行,在return之前执行
  11. * 准确说在return之间。。。
  12. */
  13. public class FinallyDemo {
  14. public static void main(String[] args) {
  15. // method1();
  16. // method2();
  17. int result = method3();
  18. System.out.println(result);
  19. }
  20. private static int method3() {
  21. int a = 10;
  22. int b = 0;
  23. try{
  24. System.out.println(a /b);
  25. System.out.println("a1.."+a);
  26. }catch(Exception e){
  27. // e.printStackTrace();
  28. a = 20;
  29. System.out.println("a2.."+a);
  30. return a;
  31. }finally{
  32. System.out.println("over");
  33. System.out.println("a3..."+a);
  34. a = 30;
  35. }
  36. return a;
  37. }
  38. private static void method2() {
  39. int a = 10;
  40. int b = 0;
  41. try{
  42. System.out.println(a /b);
  43. }catch(Exception e){
  44. e.printStackTrace();
  45. return; //在catch中加入return finally代码是否执行?
  46. }finally{
  47. System.out.println("over");
  48. }
  49. }
  50. private static void method1() {
  51. int a = 10;
  52. int b = 0;
  53. try{
  54. System.out.println(a /b);
  55. }catch(Exception e){
  56. e.printStackTrace();
  57. System.exit(0); //finally会永远执行吗?
  58. }finally{
  59. System.out.println("over");
  60. }
  61. }
  62. }

可以自己运行代码查看结果。。。。

抛出异常

  1. package exception;
  2. import java.io.FileInputStream;
  3. import java.util.Scanner;
  4. /**
  5. * @author Angus
  6. * 抛出异常
  7. * 格式;
  8. * 在方法名称后跟关键字: throws 异常类名
  9. *
  10. * 异常的分类:
  11. * A:Exception 编译时异常
  12. * B:RunTimeException 运行时异常
  13. */
  14. public class ExceptionDemo {
  15. public static void main(String[] args) {
  16. try {
  17. method();
  18. } catch (Exception e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. }
  22. }
  23. //告诉你这个方法可能有问题,需要注意。。但是没有处理。。
  24. // private static void method() throws ArithmeticException{
  25. // int a = 10;
  26. // int b = 0;
  27. // System.out.println(a /b );
  28. // }
  29. private static void method() throws Exception{
  30. int a = 10;
  31. int b = 0;
  32. System.out.println(a /b );
  33. }
  34. }

最后附上JDK使用文档API 下载

发表评论

表情:
评论列表 (有 0 条评论,153人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java基础异常Exception

    异常,是Java中非常常用的功能,它可以简化代码,并且增强代码的安全性。本文将介绍一些异常高级知识,也是学习Java一来的一次总结。包括以下内内容: 1. 异常的基础知识