• 2022年0704-Com.Java.Basis 第十三课 《Java中的异常处理》经历前面的十二课的学习:我在思考在Java中报错如何处理


    2022年0704-Com.Java.Basis  第十三课 《Java中的异常处理》经历前面的十二课的学习:我在思考在Java中报错如何处理呢!本博客以Demo为主:来了解java中的异常操作。

    在编写java代码的过程中代码报错简称异常

    java中的异常指的是什么?

    Java 中的异常(Exception)又称为例外,是一个在程序执行期间发生的事件,它中断正在执行的程序的正常指令流。为了能够及时有效地处理程序中的运行错误,必须使用异常类。

    异常实例:

    为了更好地理解什么是异常,下面来看一段非常简单的 Java 程序。下面的示例代码实现了允许用户输入 1~3 以内的整数,其他情况提示输入错误。
     

    异常产生原因:

    在 Java 中一个异常的产生,主要有如下三种原因:

    (1)Java 内部错误发生异常,Java 虚拟机产生的异常。

    (2)编写的程序代码中的错误所产生的异常,例如空指针异常、数组越界异常等。这种异常称为未检査的异常,一般需要在某些类中集中处理这些异常。

    (3)通过 throw 语句手动生成的异常,这种异常称为检査的异常,一般用来告知该方法的调用者一些必要的信息。

    异常使用原则:

    Java 异常强制用户考虑程序的强健性和安全性。异常处理不应用来控制程序的正常流程,其主要作用是捕获程序在运行时发生的异常并进行相应处理。编写代码处理某个方法可能出现的异常,可遵循如下三个原则:

    (1)在当前方法声明中使用 try catch 语句捕获异常。

    (2)一个方法被覆盖时,覆盖它的方法必须拋出相同的异常或异常的子类。

    (3)如果父类抛出多个异常,则覆盖方法必须拋出那些异常的一个子集,而不能拋出新异常。

    以上就是java中的异常指的是什么的详细内容。

     案例一

    1. package com.JavaBasicsDemo8;
    2. public class Exception extends Throwable {
    3. /**
    4. * //java.lang.ArithmaticException算数异常,被除数不能为0
    5. * Exception in thread "main" java.lang.ArithmeticException: / by zero
    6. * at com.JavaBasicsDemo8.Exception.main(Exception.java:6)
    7. * @param args
    8. */
    9. public static void main(String[] args) {
    10. int rest=4/0;
    11. System.out.println(rest);
    12. }
    13. }

     案例二

    1. package com.JavaBasicsDemo8;
    2. /**
    3. * NumberFormatException 字符转换数字异常
    4. * public static void main(String[] args) {
    5. * System.out.println("异常之前");
    6. * String str ="ert";
    7. * System.out.println(str+"年龄是:");
    8. * int age=Integer.parseInt("20L");
    9. * System.out.println(age);
    10. * System.out.println("异常之后");
    11. * }
    12. */
    13. public class Exception1 {
    14. public static void main(String[] args) {
    15. System.out.println("异常之前");
    16. String str ="ert";
    17. System.out.println(str+"年龄是:");
    18. int age=Integer.parseInt("20L");
    19. System.out.println(age);
    20. System.out.println("异常之后");
    21. }
    22. }

     

    案例三

    1. package com.JavaBasicsDemo8;
    2. public class Exception2 {
    3. public static void main(String[] args) {
    4. // TODO Auto-generated method stub
    5. System.out.println("程序开始了");
    6. int[] arr = new int[3];
    7. System.out.println(arr[3]);
    8. System.out.println("程序in the end ");
    9. }
    10. }

     案例四

    1. package com.JavaBasicsDemo8;
    2. public class Exception3 {
    3. public static void main(String[] args) {
    4. System.out.println("程序开始了");
    5. Integer i =new Integer ("abd");
    6. System.out.println("程序截止了");
    7. System.out.println("eeeeee");
    8. }
    9. }

     案例五

    1. package com.JavaBasicsDemo8;
    2. import sun.rmi.transport.proxy.RMISocketInfo;
    3. /**
    4. * Exception in thread "main" java.lang.NumberFormatException: For input string: "20l"
    5. * at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    6. * at java.lang.Integer.parseInt(Integer.java:580)
    7. * at java.lang.Integer.parseInt(Integer.java:615)
    8. * at com.JavaBasicsDemo8.Exception4.main(Exception4.java:12)
    9. */
    10. /**
    11. * try-catch
    12. */
    13. public class Exception4 {
    14. public static void main(String[] args) {
    15. String str="lili";
    16. System.out.println(str+"年龄:");
    17. int age=Integer.parseInt("20l");
    18. System.out.println(age);
    19. System.out.println("program");
    20. }
    21. }

     案例六

    1. package com.JavaBasicsDemo8;
    2. public class ExceptionDemo1 {
    3. public static void main(String[] args) {
    4. System.out.println("程序开始了");
    5. try {
    6. //Integer i =new Integer ("abd");
    7. int a=1/0;
    8. int [] arr =new int[3];
    9. System.out.println(arr[3]);
    10. System.out.println(arr[1]);
    11. System.out.println("程序in the end");
    12. }catch(ArrayIndexOutOfBoundsException e) {
    13. System.out.println("数组下标越界");
    14. //打印异常信息
    15. e.printStackTrace();
    16. }catch(ArithmeticException e ) {
    17. System.out.println("除数不为零");
    18. e.printStackTrace();
    19. }
    20. System.out.println("程序结束了");
    21. }
    22. }

    案例七

    1. package com.JavaBasicsDemo8;
    2. public class ExceptionDemo2 {
    3. public static void main(String[] args) {
    4. System.out.println("程序开始了");
    5. try {
    6. Integer i =new Integer ("abd");
    7. int a=1/0;
    8. int [] arr =new int[3];
    9. System.out.println(arr[3]);
    10. System.out.println(arr[1]);
    11. System.out.println("程序in the end");
    12. }catch(ArrayIndexOutOfBoundsException e) {
    13. System.out.println("数组下标越界");
    14. //打印异常信息
    15. e.printStackTrace();
    16. }catch(ArithmeticException r ) {
    17. System.out.println("除数不为零");
    18. }catch(NumberFormatException c) {
    19. System.out.println("数字转款异常");
    20. c.printStackTrace();
    21. }finally{
    22. System.out.println("程序结束了,可以下班了吧");
    23. System.out.println("GootByate");
    24. }
    25. }
    26. }

     

    案例八

    1. package com.JavaBasicsDemo8;
    2. public class ExceptionDemo3 {
    3. public static void main(String[] args) {
    4. System.out.println("程序开始了");
    5. try {
    6. Integer i =new Integer ("abd");
    7. int a=1/0;
    8. int [] arr =new int[3];
    9. System.out.println(arr[3]);
    10. System.out.println(arr[1]);
    11. System.out.println("程序in the end");
    12. }catch(ArrayIndexOutOfBoundsException e) {
    13. System.out.println("数组下标越界");
    14. //打印异常信息
    15. e.printStackTrace();
    16. }catch(ArithmeticException r ) {
    17. System.out.println("除数不为零");
    18. }catch(NumberFormatException c) {
    19. System.out.println("数字转款异常");
    20. c.printStackTrace();
    21. System.out.println(c.getMessage());
    22. }finally{
    23. System.out.println("程序结束了");
    24. }
    25. }
    26. }

     

    案例九

    1. package com.JavaBasicsDemo8;
    2. import java.text.ParseException;
    3. import java.text.SimpleDateFormat;
    4. import java.util.Date;
    5. public class ExceptionDemo4 {
    6. public static void main(String[] args) {
    7. System.out.println("程序开始");
    8. method();
    9. try {
    10. method1();
    11. }catch(ArrayIndexOutOfBoundsException e) {
    12. e.printStackTrace();
    13. }
    14. System.out.println("程序结束了哦哦哦");
    15. }
    16. public static void method() {
    17. String s ="2021-05-01";
    18. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
    19. try {
    20. Date date = sdf.parse(s);
    21. }catch(ParseException r) {
    22. r.printStackTrace();
    23. }
    24. }
    25. //谁调用就跟谁
    26. public static void method1() throws ArrayIndexOutOfBoundsException{
    27. int [] arr =new int [3];
    28. System.out.println(arr[3]);
    29. }
    30. }

     

    案例十

    1. package com.JavaBasicsDemo8;
    2. public class ExceptionDemo5 {
    3. public static void main(String[] args) {
    4. try {
    5. pop();
    6. }catch (NegativeArraySizeException e){
    7. System.out.println("pop()方法抛出异常");
    8. }
    9. }
    10. private static void pop() throws NegativeArraySizeException{
    11. int arr[]=new int[-4];
    12. }
    13. }

     

    案例十一

    1. package com.MyException;
    2. public class Tran {
    3. static int avg(int number1,int number2) throws MyException{
    4. if(number1<0||number2<0){
    5. throw new MyException("不能为负数");
    6. }
    7. if (number1 > 100||number2>100) {
    8. throw new MyException("数值大了");
    9. }
    10. return (number1+number2)/2;
    11. }
    12. public static void main(String[] args) {
    13. try {
    14. int result =avg(102,23);
    15. System.out.println(result);
    16. }catch (MyException e) {
    17. System.out.println(e);
    18. }
    19. }
    20. }

    案例十二

    1. package com.MyException;
    2. public class Student {
    3. public void checkAge(int age ) throws AgeException {
    4. if(age<0||age>200) {
    5. //报异常
    6. throw new AgeException("你的年龄在1到200中间");
    7. }
    8. }
    9. public void checkname(int name) throws NameException {
    10. name=1; //报异常
    11. throw new NameException("你的名字只能为小于五个字");
    12. }
    13. public void checknum(String num) throws NumException {
    14. num="a-z,A-Z";
    15. //报异常
    16. throw new NumException("你输入的数字不能为");
    17. }
    18. public void checknum(int num) {
    19. // TODO Auto-generated method stub
    20. }
    21. }
    1. package com.MyException;
    2. public class AgeException extends Exception {
    3. public AgeException() {
    4. super();
    5. // TODO Auto-generated constructor stub
    6. }
    7. public AgeException(String string) {
    8. super();
    9. // TODO Auto-generated constructor stub
    10. }
    11. }
    1. package com.MyException;
    2. /**
    3. * 自定义异常
    4. */
    5. public class MyException extends Exception{
    6. public MyException(String ErrorMessage){
    7. super(ErrorMessage);
    8. }
    9. }
    1. package com.MyException;
    2. public class NameException extends Exception{
    3. public NameException() {
    4. super();
    5. }
    6. public NameException(String wert) {
    7. super(wert);
    8. // TODO Auto-generated constructor stub
    9. }
    10. }
    1. package com.MyException;
    2. public class NumException extends Exception {
    3. public NumException() {
    4. super();
    5. }
    6. public NumException(String numrt) {
    7. super(numrt);
    8. // TODO Auto-generated constructor stub
    9. }
    10. }
    1. package com.MyException;
    2. import java.util.Scanner;
    3. public class StudentTest {
    4. public static void main(String[] args) throws NameException, AgeException {
    5. //
    6. Scanner scanner =new Scanner(System.in);
    7. System.out.println("请输入你的年龄");
    8. int age =scanner.nextInt();
    9. //创建对象
    10. Student stu =new Student();
    11. try {
    12. stu.checkAge(age);
    13. }catch(AgeException e){
    14. e.printStackTrace();
    15. }finally {
    16. System.out.println("qqqqqqqqqqqqqqqqqqq");
    17. }
    18. Scanner n =new Scanner(System.in);
    19. System.out.println("请输入你的名字");
    20. int name =scanner.nextInt();
    21. //创建对象2
    22. Student stu2=new Student();
    23. try {
    24. stu2.checkname(name);
    25. }catch(NameException f){
    26. f.printStackTrace();
    27. }
    28. }
    29. }
    1. package com.MyException;
    2. public class Tran {
    3. static int avg(int number1,int number2) throws MyException{
    4. if(number1<0||number2<0){
    5. throw new MyException("不能为负数");
    6. }
    7. if (number1 > 100||number2>100) {
    8. throw new MyException("数值大了");
    9. }
    10. return (number1+number2)/2;
    11. }
    12. public static void main(String[] args) {
    13. try {
    14. int result =avg(102,23);
    15. System.out.println(result);
    16. }catch (MyException e) {
    17. System.out.println(e);
    18. }
    19. }
    20. }

     Java的异常有很多我不可能将所有的异常跟你总结起来:像这类情况还是要看自己的积累

  • 相关阅读:
    2.2python 常用的数据结构之列表(list)_python量化实用版教程(初级)
    SQL2 查询多列
    【C++】多态面试题
    2023计算机毕业设计SSM最新选题之java乡村振兴战略背景下乡村教师培训教学系统fwmvy
    2311rust,到54版本更新
    redis中value/set
    window10环境下搭建ros
    Tomcat下载安装以及配置(详细教程)
    列表与字典—>一维列表
    谷歌公开自动驾驶新专利:通过眼睛注视向量,精确判断注意力
  • 原文地址:https://blog.csdn.net/qq_56248592/article/details/125596074