广告位联系
返回顶部
分享到

Java基础:异常处理操作

java 来源:互联网搜集 作者:秩名 发布时间:2019-08-24 17:39:00 人浏览
摘要

示例代码: public class ExecDemo { public static void main(String[] args) { int[] nums = new int[4]; System.out.println(before the exception:); try { //try代码块 try catch代码块可以嵌套 try{ nums[7] = 10; //数组越界 System.out.println(no e

示例代码:
 

 
public class ExecDemo {
  public static void main(String[] args) {
    int[] nums = new int[4];
    System.out.println("before the exception:");
    try {  //try代码块  try catch代码块可以嵌套
      try{
         nums[7] = 10;  //数组越界
         System.out.println("no exception:");
      }catch(ArithmeticException e){
         e.printStackTrace();
      }
    }catch(ArithmeticException e) { //catch代码块  多个catch块
      e.printStackTrace();  //打印异常信息
    }catch(ArrayIndexOutOfBoundsException e){  // 捕获数组越界错误  捕获子类异常
       e.printStackTrace();
    }catch(Throwable e){  //捕获超类异常 Throwable是所有异常的超类
       e.printStackTrace();
    }
    System.out.println("after the exception");
  }
}

抛出异常:

 
public class ThrowDemo {
  public static void main(String[] args) {
    try {
      System.out.println("before throw:");
      throw new ArithmeticException();  //throw关键字抛出一个ArithmeticException()异常(手动抛出异常)
    } catch (ArithmeticException e) {  //捕获异常
      System.out.println("exception caught:");
    }
    System.out.println("after try{}catch{}:");
  }
}

重新抛出异常:

 
class Rethrow {
  public static void genException() {
    int[] numer = {2,4,6,8,10,12};
    int[] demon = {2,0,3,4,0};
      for(int i=0;i < numer.length;i++){
        try {                                   //try代码块
          System.out.println(numer[i]/demon[i]);
        } catch (ArithmeticException e) {  //多个catch()块
          System.out.println("can't dev by zero:");
        }catch(ArrayIndexOutOfBoundsException e) {
          System.out.println("no error:");
          throw e;  //throw 重写抛出异常
        }
      }
  }
}
public class RethrowDemo{
  public static void main(String args[]) {
    try {
      Rethrow.genException();
    } catch (ArrayIndexOutOfBoundsException e) {  //捕获重新抛出的异常
      System.out.println("error error error error error:");
    }
    finally{  //finally代码块在try catch执行完时执行的。
       System.out.println("Leaving try.");
     }
  }
}

throws语句:一个方法产生自己不做处理的异常,用throws抛出到外层(谁调用,谁处理异常)

 
public class ThrowsDemo {
  public static char prompt(String str) throws java.io.IOException{//prompt()方法产生自己不做处理的IOException异常,抛出到外层,谁调用谁处理异常
    System.out.print(str +":");
    return (char) System.in.read();
  }
  public static void main(String[] args) {
    char ch;
    try {
      ch = prompt("enter a letter");  //prompt()可能抛出异常,
    } catch (java.io.IOException e) {  //捕获prompt()抛出的异常
      System.out.println("IOException occurred");
      ch = 'x';
    }
    System.out.println("you pressed:"+ ch);
  }
}

可以用一个catch()捕获多个异常:

 
try{

}
catch(ArithmeticException|ArrayIndexOutOfBoundsException e){//同时捕获多个异常
}

自定义异常:

 
class NonIntResultException extends Exception{  //自定义异常继承子Exception
  int n ,d;
  NonIntResultException(int i,int j){
    n = i;
    d = j;
  }
  public String toString() {
    return "result of "+ n +"/"+ d +" is non-integer.";
  }
}
public class CustomExceptionDemo {
  public static void main(String[] args) {
    int numer[] = {4,8,15,32,64,127,256,512};
    int denom[] = {2,0,4,4,0,8};
    for(int i=0;i<numer.length;i++) {
      try {
        if((numer[i]%2)!=0) {
          throw new NonIntResultException(numer[i],denom[i]);  //抛出自定义异常
        }
        System.out.println(numer[i] +"/"+ denom[i] +" is "+ numer[i]/denom[i]);
      } catch (ArithmeticException e) {  //捕获ArithmeticException异常
        System.out.println("can't divide by zero!");
      }catch (ArrayIndexOutOfBoundsException e) { //捕获ArrayIndexOutOfBoundsException 异常
        System.out.println("no matching element found.");
      }catch (NonIntResultException e) {  //捕获自定义异常
        System.out.println(e);
      }
    }
  }
}
 


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/QQhelphelp/article/details/87950322
相关文章
  • SpringBoot自定义错误处理逻辑介绍

    SpringBoot自定义错误处理逻辑介绍
    1. 自定义错误页面 将自定义错误页面放在 templates 的 error 文件夹下,SpringBoot 精确匹配错误信息,使用 4xx.html 或者 5xx.html 页面可以打印错误
  • Java实现手写一个线程池的代码

    Java实现手写一个线程池的代码
    线程池技术想必大家都不陌生把,相信在平时的工作中没有少用,而且这也是面试频率非常高的一个知识点,那么大家知道它的实现原理和
  • Java实现断点续传功能的代码

    Java实现断点续传功能的代码
    题目实现:网络资源的断点续传功能。 二、解题思路 获取要下载的资源网址 显示网络资源的大小 上次读取到的字节位置以及未读取的字节
  • 你可知HashMap为什么是线程不安全的
    HashMap 的线程不安全 HashMap 的线程不安全主要体现在下面两个方面 在 jdk 1.7 中,当并发执行扩容操作时会造成环形链和数据丢失的情况 在
  • ArrayList的动态扩容机制的介绍

    ArrayList的动态扩容机制的介绍
    对于 ArrayList 的动态扩容机制想必大家都听说过,之前的文章中也谈到过,不过由于时间久远,早已忘却。 所以利用这篇文章做做笔记,加
  • JVM基础之字节码的增强技术介绍

    JVM基础之字节码的增强技术介绍
    字节码增强技术 在上文中,着重介绍了字节码的结构,这为我们了解字节码增强技术的实现打下了基础。字节码增强技术就是一类对现有字
  • Java中的字节码增强技术

    Java中的字节码增强技术
    1.字节码增强技术 字节码增强技术就是一类对现有字节码进行修改或者动态生成全新字节码文件的技术。 参考地址 2.常见技术 技术分类 类
  • Redis BloomFilter布隆过滤器原理与实现

    Redis BloomFilter布隆过滤器原理与实现
    Bloom Filter 概念 布隆过滤器(英语:Bloom Filter)是1970年由一个叫布隆的小伙子提出的。它实际上是一个很长的二进制向量和一系列随机映射
  • Java C++算法题解leetcode801使序列递增的最小交换次

    Java C++算法题解leetcode801使序列递增的最小交换次
    题目要求 思路:状态机DP 实现一:状态机 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public int minSwap(int[] nums1, int[] nums2) { int n
  • Mybatis结果集映射与生命周期介绍

    Mybatis结果集映射与生命周期介绍
    一、ResultMap结果集映射 1、设计思想 对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了 2、resultMap的应用场
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计