异常就是指程序抛出的问题

问题分类

错误

Error 错误不能算为异常,因为它,程序员是没办法处理的。例如:内存不足,硬盘空间不够,网线被耗子拆了,服务器停电了……

异常

Exception 就程序员通过代码是可以解决的问题。

编译期异常

指在编写代码过程,由编译期主动识别出来的问题,就是编译期异常
异常 - 图1

运行时异常

指在编写代码过程中,编译期识别不出来,但是在运行期由JVM抛出来的问题
异常 - 图2

异常的解决方案

运行时异常解决方案

不管它,让它抛,抛出来以后再解决!!!

编译时异常解决方案

编译时异常有3种解决方案:
1、捕获这个异常,不让它沿着调用栈继续向下抛出

  1. ``
  2. private static void method02() {
  3. try {
  4. //编译时异常
  5. File file = new File("D://test.txt");
  6. FileInputStream in = new FileInputStream(file);
  7. }catch (Exception e) {
  8. // TODO: handle exception
  9. e.printStackTrace();
  10. }
  11. }

2、捕获这个异常,并继续向下抛出;

  1. private static void method02() throws Exception{
  2. try {
  3. //编译时异常
  4. File file = new File("D://test.txt");
  5. FileInputStream in = new FileInputStream(file);
  6. }catch (Exception e) {
  7. // TODO: handle exception
  8. e.printStackTrace();
  9. throw e;
  10. }
  11. }

3、不捕获这个异常,从而导致method1()方法从调用栈中被弹出,异常对象继续抛给调用栈下面的main()方法。

  1. private static void method02() throws Exception{
  2. //编译时异常
  3. File file = new File("D://test.txt");
  4. FileInputStream in = new FileInputStream(file);
  5. }

方法只要向下抛出异常,将会自动从栈结构中被弹出
异常 - 图3

怎么识别异常?

异常 - 图4
凡是直接继承Exceptioin类,或者是它子类的派生几乎都是编译时异常
凡是直接继承RuntimeException类, 或者是它子类的派生都是运行时异常

如何捕获异常

语法:

  1. try {
  2. //监控代码,抛不抛异常
  3. }catch(Exception e){
  4. //如何解决异常
  5. }

案例:

  1. private static void method02() {
  2. System.out.println("开始");
  3. // 编译时异常
  4. File file = new File("D://test.txt");
  5. ``
  6. try {
  7. FileInputStream in = new FileInputStream(file);
  8. //如果上述代码,抛出异常,下面的代码不会执行
  9. System.out.println("asdfasdfasdfasdasdf");
  10. ``
  11. } catch (FileNotFoundException e) {
  12. // TODO Auto-generated catch block
  13. e.printStackTrace();
  14. System.out.println("抛异常……");
  15. }
  16. ``
  17. System.out.println("结束");
  18. }

    自定义编译时异常

    extends Exception完事

  19. /**

  20. * 自定义 编译期异常
  21. * @author 蜗牛学院
  22. *
  23. */
  24. public class InitException extends Exception{
  25. ``
  26. public InitException() {
  27. super();
  28. // TODO Auto-generated constructor stub
  29. }
  30. ``
  31. public InitException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
  32. super(message, cause, enableSuppression, writableStackTrace);
  33. // TODO Auto-generated constructor stub
  34. }
  35. ``
  36. public InitException(String message, Throwable cause) {
  37. super(message, cause);
  38. // TODO Auto-generated constructor stub
  39. }
  40. ``
  41. public InitException(String message) {
  42. super(message);
  43. // TODO Auto-generated constructor stub
  44. }
  45. ``
  46. public InitException(Throwable cause) {
  47. super(cause);
  48. // TODO Auto-generated constructor stub
  49. }
  50. }/**
  51. * 自定义 编译期异常
  52. * @author 蜗牛学院
  53. *
  54. */
  55. public class InitException extends Exception{
  56. ``
  57. public InitException() {
  58. super();
  59. // TODO Auto-generated constructor stub
  60. }
  61. ``
  62. public InitException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
  63. super(message, cause, enableSuppression, writableStackTrace);
  64. // TODO Auto-generated constructor stub
  65. }
  66. ``
  67. public InitException(String message, Throwable cause) {
  68. super(message, cause);
  69. // TODO Auto-generated constructor stub
  70. }
  71. ``
  72. public InitException(String message) {
  73. super(message);
  74. // TODO Auto-generated constructor stub
  75. }
  76. ``
  77. public InitException(Throwable cause) {
  78. super(cause);
  79. // TODO Auto-generated constructor stub
  80. }
  81. }

如何使用异常?
需要定义一个方法,在方法中使用throw 将异常抛出,但是一定要结合throws对该方法进行异常定义

  1. private static int add(int a,int b) throws InitException {
  2. if(a < 0 || b < 0) {
  3. throw new InitException("传入的参数为:负数");
  4. }
  5. ``
  6. return a + b;
  7. ``
  8. }
  9. ``
  10. public static void main(String[] args) {
  11. // TODO Auto-generated method stub
  12. ``
  13. try {
  14. add(4,5);
  15. } catch (InitException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. }

<br />