Java:认识非常

打印 上一主题 下一主题

主题 1954|帖子 1954|积分 5862

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在Java中我们把步伐实验过程中不正常的举动称为非常。
一 常见的非常

1.算数非常

  1. public class Test {
  2.     public static void main(String[] args) {
  3.         int a=10;
  4.         System.out.println(a/0);
  5.     }
  6. }
复制代码

2.数组越界非常

  1. public class Test {
  2.     public static void main(String[] args) {
  3.         char[] array={1,2,3};
  4.         System.out.println(array[5]);
  5.     }
复制代码

3.空指针非常

  1. public class Test {
  2.     public static void main(String[] args) {
  3.         char[] array=null;
  4.         System.out.println(array.length);
  5.     }
复制代码

二 非常的体系结构 


1.throwable是非常类的顶层类,是全部非常类的父类 ,Error 和 Exception
2.Error:指的是Java虚拟机无法解决的严重问题,好比:JVM的内部错误、资源耗尽等,典范代表: StackOverflowError和OutOfMemoryError,一旦发生回力乏术。
3.Exception:指的是步伐员可以通过修改代码处理的非常。
三 非常的分类

1.编译时非常

一般发生在编译期间,也称为受查抄非常(Checked Exception)
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         Person person=new Person();
  4.         Person person1=(Person)person.clone();
  5.     }
复制代码
 

2.运行时非常

在运行之后才会显示的非常 
四 非常的处理

在Java中,非常时根据一些关键词来处理的,try,catch,finally,throw,throws。这些关键词提供了结构化的方法来处理运行时的非常。
1.try-catch结构

try 关键字用于包罗可能会抛出非常的代码块,catch 用于捕获和处理非常。
根本结构
  1. try {
  2.     // 可能会抛出异常的代码
  3. } catch (ExceptionType1 e1) {
  4.     // 处理 ExceptionType1 异常
  5. } catch (ExceptionType2 e2) {
  6.     // 处理 ExceptionType2 异常
  7. } finally {
  8.     // 无论是否抛出异常,finally 代码块都会执行
  9. }
复制代码
 示例:

  1. public class Test {
  2.     public static void function(int a)throws CloneNotSupportedException{
  3.         if(a==10){
  4.             throw new CloneNotSupportedException();
  5.         }
  6.     }
  7.     public static void main(String[] args) {
  8.         try {
  9.             function(10);
  10.             System.out.println("符合了异常条件");
  11.         }catch(CloneNotSupportedException e){
  12.             System.out.println("捕获了异常,处理异常");
  13.         }finally {
  14.             System.out.println("lalalalala");
  15.         }
  16.         System.out.println("没有问题,程序正常运行");
  17.     }
复制代码
注:如果有非常,则抛出非常的代码块,非常后面的代码无法运行。
       finally 代码块无论何时都会实验。
2.多重捕获

如果代码匹配哪个非常,则会进行哪个catch块进行处理
  1.     public static void main(String[] args) {
  2.         try {
  3.             int[] arr=new int[3];
  4.             arr[5]=8;
  5.         }catch (ArithmeticException e){
  6.             System.out.println("算数异常"+e.getMessage());
  7.         }catch (ArrayIndexOutOfBoundsException e){
  8.             System.out.println("数组越界异常"+e.getMessage());
  9.         }finally {
  10.             System.out.println("结束");
  11.         }
  12.     }
复制代码
如果没有匹配到合适的catch,代码则会继续向上实验,直到合适的地方捕获到非常,或者直接崩溃。 
注:
在Java 7之后答应一个代码块实验多个非常
  1.         public static void main(String[] args){
  2.             try {
  3.                 int[] arr = new int[3];
  4.                 arr[5] = 8;
  5.             } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
  6.                 System.out.println("算数异常" + e.getMessage() + "数组越界异常" + e.getMessage());
  7.             } finally {
  8.                 System.out.println("结束");
  9.             }
  10.         }
复制代码

3.finally块

finally块的代码无论是否抛出非常,都会被实验,通常用于清理资源,如关闭文件、释放数据库毗连等。如果try-catch中有return,代码块也会被实验。
  1.     public static void main(String[] args) {
  2.         try {
  3.             int[] arr=new int[3];
  4.             arr[5]=8;
  5.         }catch (ArithmeticException e){
  6.             System.out.println("算数异常"+e.getMessage());
  7.             return;
  8.         }catch (ArrayIndexOutOfBoundsException e){
  9.             System.out.println("数组越界异常"+e.getMessage());
  10.             return;
  11.         }finally {
  12.             System.out.println("结束");
  13.         }
  14.     }
复制代码

4. throw

 throw关键词用于手动抛出非常,可以被用于任何地方
  1. public static void sex(String sex){
  2.         if(sex=="男"){
  3.             throw new ArithmeticException("不能进入女子队");
  4.         }else {
  5.             System.out.println("可以进入女子队伍");
  6.         }
  7. }
  8.     public static void main(String[] args) {
  9.         sex("男");
  10.     }
复制代码

满意性别为男性时,步伐手动抛出 ArithmeticException。 
5.throws

throws关键词用于声明,体现该方法可能抛出的哪些非常,被标记的非常要么被捕捉,要么在方法调用的地方继续抛出。
  1. public static void ReadFile() throws IOException {
  2.     FileReader fileReader=new FileReader("text.c");
  3. }
  4.     public static void main(String[] args) throws IOException{
  5.         ReadFile();
  6.     }
复制代码
6.自界说非常 

Java 答应你界说自己的非常类,通常通过继承 Exception 或 RuntimeException 来实现。自界说非常可以帮助你根据业务逻辑界说特殊的错误环境。
如果不用自界说非常的方法写一个登录途径

  1. class login{
  2.     private String name;
  3.     private String num;
  4.     public String getName() {
  5.         return name;
  6.     }
  7.     public void setName(String name) {
  8.         this.name = name;
  9.     }
  10.     public String getNum() {
  11.         return num;
  12.     }
  13.     public void setNum(String num) {
  14.         this.num = num;
  15.     }
  16.     public static void login(String name,String num ){
  17.         Scanner scanner=new Scanner(System.in);
  18.         if(!name.equals(name)){
  19.             System.out.println("姓名输入错误");
  20.             return;
  21.         }else if (!num.equals(num)){
  22.             System.out.println("账号输入错误");
  23.             return;
  24.         }
  25.         System.out.println("登陆成功");
  26.     }
  27.     public static void main(String[] args) {
  28.         login("zhangsan","123456");
  29.     }
  30. }
复制代码

使用自界说非常

  1. class NameException extends Exception {
  2.     public NameException(String message) {
  3.         super(message);
  4.     }
  5. }
  6. class NumException extends Exception {
  7.     public NumException(String message) {
  8.         super(message);
  9.     }
  10. }
  11. class login{
  12.     private String name;
  13.     private String num;
  14.     public String getName() {
  15.         return name;
  16.     }
  17.     public void setName(String name) {
  18.         this.name = name;
  19.     }
  20.     public String getNum() {
  21.         return num;
  22.     }
  23.     public void setNum(String num) {
  24.         this.num = num;
  25.     }
  26.     public static void login(String name,String num ) throws NameException,NumException{
  27.         Scanner scanner=new Scanner(System.in);
  28.         if(!name.equals(name)) {
  29.             throw new NameException("姓名输入错误");
  30.         }else if (!num.equals(num)){
  31.             throw new NumException("账号输入错误");
  32.         }
  33.         System.out.println("登陆成功");
  34.     }
  35.     public static void main(String[] args) {
  36.         try {
  37.             login("zhangsan","123456");
  38.         }catch (NameException e){
  39.             e.printStackTrace();
  40.         }catch (NumException e){
  41.             e.printStackTrace();
  42.         }
  43.     }
  44. }
复制代码
7. 总结:



  • try-catch-finally 是非常处理的核心结构,try 用于包罗可能抛出非常的代码,catch 用于捕获和处理非常,finally 块用于清理资源。
  • throw 用于手动抛出非常,而 throws 用于声明方法可能抛出的非常。
  • 处理非常时,建议只捕获你能处理的非常,避免捕获过于笼统的非常,如直接捕获 Exception,如许会掩饰潜在的编程错误。
  • finally 块应尽可能用于释放资源,避免资源泄露问题。
  • 通过自界说非常,可以根据业务需求界说特定的非常来增强代码的可读性和可维护性。
 希望能对各人有所帮助!!!!!!!







 




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表