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中的异常指的是什么的详细内容。
案例一
package com.JavaBasicsDemo8; public class Exception extends Throwable { /** * //java.lang.ArithmaticException算数异常,被除数不能为0 * Exception in thread "main" java.lang.ArithmeticException: / by zero * at com.JavaBasicsDemo8.Exception.main(Exception.java:6) * @param args */ public static void main(String[] args) { int rest=4/0; System.out.println(rest); } }
案例二
package com.JavaBasicsDemo8; /** * NumberFormatException 字符转换数字异常 * public static void main(String[] args) { * System.out.println("异常之前"); * String str ="ert"; * System.out.println(str+"年龄是:"); * int age=Integer.parseInt("20L"); * System.out.println(age); * System.out.println("异常之后"); * } */ public class Exception1 { public static void main(String[] args) { System.out.println("异常之前"); String str ="ert"; System.out.println(str+"年龄是:"); int age=Integer.parseInt("20L"); System.out.println(age); System.out.println("异常之后"); } }
案例三
package com.JavaBasicsDemo8; public class Exception2 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("程序开始了"); int[] arr = new int[3]; System.out.println(arr[3]); System.out.println("程序in the end "); } }
案例四
package com.JavaBasicsDemo8; public class Exception3 { public static void main(String[] args) { System.out.println("程序开始了"); Integer i =new Integer ("abd"); System.out.println("程序截止了"); System.out.println("eeeeee"); } }
案例五
package com.JavaBasicsDemo8; import sun.rmi.transport.proxy.RMISocketInfo; /** * Exception in thread "main" java.lang.NumberFormatException: For input string: "20l" * at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) * at java.lang.Integer.parseInt(Integer.java:580) * at java.lang.Integer.parseInt(Integer.java:615) * at com.JavaBasicsDemo8.Exception4.main(Exception4.java:12) */ /** * try-catch */ public class Exception4 { public static void main(String[] args) { String str="lili"; System.out.println(str+"年龄:"); int age=Integer.parseInt("20l"); System.out.println(age); System.out.println("program"); } }
案例六
package com.JavaBasicsDemo8; public class ExceptionDemo1 { public static void main(String[] args) { System.out.println("程序开始了"); try { //Integer i =new Integer ("abd"); int a=1/0; int [] arr =new int[3]; System.out.println(arr[3]); System.out.println(arr[1]); System.out.println("程序in the end"); }catch(ArrayIndexOutOfBoundsException e) { System.out.println("数组下标越界"); //打印异常信息 e.printStackTrace(); }catch(ArithmeticException e ) { System.out.println("除数不为零"); e.printStackTrace(); } System.out.println("程序结束了"); } }
案例七
package com.JavaBasicsDemo8; public class ExceptionDemo2 { public static void main(String[] args) { System.out.println("程序开始了"); try { Integer i =new Integer ("abd"); int a=1/0; int [] arr =new int[3]; System.out.println(arr[3]); System.out.println(arr[1]); System.out.println("程序in the end"); }catch(ArrayIndexOutOfBoundsException e) { System.out.println("数组下标越界"); //打印异常信息 e.printStackTrace(); }catch(ArithmeticException r ) { System.out.println("除数不为零"); }catch(NumberFormatException c) { System.out.println("数字转款异常"); c.printStackTrace(); }finally{ System.out.println("程序结束了,可以下班了吧"); System.out.println("GootByate"); } } }
案例八
package com.JavaBasicsDemo8; public class ExceptionDemo3 { public static void main(String[] args) { System.out.println("程序开始了"); try { Integer i =new Integer ("abd"); int a=1/0; int [] arr =new int[3]; System.out.println(arr[3]); System.out.println(arr[1]); System.out.println("程序in the end"); }catch(ArrayIndexOutOfBoundsException e) { System.out.println("数组下标越界"); //打印异常信息 e.printStackTrace(); }catch(ArithmeticException r ) { System.out.println("除数不为零"); }catch(NumberFormatException c) { System.out.println("数字转款异常"); c.printStackTrace(); System.out.println(c.getMessage()); }finally{ System.out.println("程序结束了"); } } }
案例九
package com.JavaBasicsDemo8; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ExceptionDemo4 { public static void main(String[] args) { System.out.println("程序开始"); method(); try { method1(); }catch(ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } System.out.println("程序结束了哦哦哦"); } public static void method() { String s ="2021-05-01"; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd"); try { Date date = sdf.parse(s); }catch(ParseException r) { r.printStackTrace(); } } //谁调用就跟谁 public static void method1() throws ArrayIndexOutOfBoundsException{ int [] arr =new int [3]; System.out.println(arr[3]); } }
案例十
package com.JavaBasicsDemo8; public class ExceptionDemo5 { public static void main(String[] args) { try { pop(); }catch (NegativeArraySizeException e){ System.out.println("pop()方法抛出异常"); } } private static void pop() throws NegativeArraySizeException{ int arr[]=new int[-4]; } }
案例十一
package com.MyException; public class Tran { static int avg(int number1,int number2) throws MyException{ if(number1<0||number2<0){ throw new MyException("不能为负数"); } if (number1 > 100||number2>100) { throw new MyException("数值大了"); } return (number1+number2)/2; } public static void main(String[] args) { try { int result =avg(102,23); System.out.println(result); }catch (MyException e) { System.out.println(e); } } }
案例十二
package com.MyException; public class Student { public void checkAge(int age ) throws AgeException { if(age<0||age>200) { //报异常 throw new AgeException("你的年龄在1到200中间"); } } public void checkname(int name) throws NameException { name=1; //报异常 throw new NameException("你的名字只能为小于五个字"); } public void checknum(String num) throws NumException { num="a-z,A-Z"; //报异常 throw new NumException("你输入的数字不能为"); } public void checknum(int num) { // TODO Auto-generated method stub } }
package com.MyException; public class AgeException extends Exception { public AgeException() { super(); // TODO Auto-generated constructor stub } public AgeException(String string) { super(); // TODO Auto-generated constructor stub } }
package com.MyException; /** * 自定义异常 */ public class MyException extends Exception{ public MyException(String ErrorMessage){ super(ErrorMessage); } }
package com.MyException; public class NameException extends Exception{ public NameException() { super(); } public NameException(String wert) { super(wert); // TODO Auto-generated constructor stub } }
package com.MyException; public class NumException extends Exception { public NumException() { super(); } public NumException(String numrt) { super(numrt); // TODO Auto-generated constructor stub } }
package com.MyException; import java.util.Scanner; public class StudentTest { public static void main(String[] args) throws NameException, AgeException { // Scanner scanner =new Scanner(System.in); System.out.println("请输入你的年龄"); int age =scanner.nextInt(); //创建对象 Student stu =new Student(); try { stu.checkAge(age); }catch(AgeException e){ e.printStackTrace(); }finally { System.out.println("qqqqqqqqqqqqqqqqqqq"); } Scanner n =new Scanner(System.in); System.out.println("请输入你的名字"); int name =scanner.nextInt(); //创建对象2 Student stu2=new Student(); try { stu2.checkname(name); }catch(NameException f){ f.printStackTrace(); } } }
package com.MyException; public class Tran { static int avg(int number1,int number2) throws MyException{ if(number1<0||number2<0){ throw new MyException("不能为负数"); } if (number1 > 100||number2>100) { throw new MyException("数值大了"); } return (number1+number2)/2; } public static void main(String[] args) { try { int result =avg(102,23); System.out.println(result); }catch (MyException e) { System.out.println(e); } } }
Java的异常有很多我不可能将所有的异常跟你总结起来:像这类情况还是要看自己的积累