美文网首页
几种常见的运行时异常实例

几种常见的运行时异常实例

作者: 矮油不错哦_ab60 | 来源:发表于2019-02-06 23:44 被阅读7次

需要导入一个mysql的驱动

Runtime Exception

运行时异常

  1. 类型转换异常
  2. 空指针异常 ————利用了Hashtable不能为空的原理。
  3. 文件找不到异常
  4. 类找不到异常
  5. 数组大小为负数异常
  6. 数组越界异常
  7. 同步修改异常

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

class Animal {

}

/**
 * 运行时异常: 类型转换异常 空指针异常 文件找不到异常 类找不到异常 数组大小为负数异常  数组越界异常 同步修改异常
 * 
 * @author 邢质坦 2019年2月6日 下午8:21:02
 */
public class TestDemo {

    public static void main(String[] args) {

        // 类型转换异常——java.lang.ClassCastException
        Object o = new TestDemo();
        Animal animal = (Animal) o;

        // 空指针异常——java.lang.NullPointerException
        Hashtable<Long, String> hashtable = new Hashtable<Long, String>();
        hashtable.put(null, null);

        FileInputStream fileInputStream = null;

        // 文件找不到异常——java.io.FileNotFoundException
        /*
         * File file = new File("xx.xx"); try { fileInputStream = new
         * FileInputStream(file); } catch (FileNotFoundException e1) { // TODO
         * Auto-generated catch block e1.printStackTrace(); }finally{
         * if(null!=fileInputStream){ try { fileInputStream.close(); } catch
         * (IOException e) { // TODO Auto-generated catch block
         * e.printStackTrace(); } } }
         */

        // 数组越界异常——java.lang.ArrayIndexOutOfBoundsException
        int[] arr = { 1, 2, 3, 4, 5 };

        // int j = arr[5];

        // 类找不到异常
        try {
            // java.lang.ClassNotFoundException
            // Class.forName("com");
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 数组大小为负数异常
        // java.lang.NegativeArraySizeException
        // int[] a = new int[-1];

        // 同步修改异常——java.util.ConcurrentModificationException
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            arrayList.add(i);
        }
        for (int x : arrayList) {
            System.out.println(x);
            // arrayList.remove(x);//java.util.ConcurrentModificationException
        }

    }

}

相关文章

  • 几种常见的运行时异常实例

    需要导入一个mysql的驱动 Runtime Exception 运行时异常 类型转换异常 空指针异常 ————利...

  • Android常见异常

    Android常见异常 异常分为编译时异常和运行时异常,当前主要说一下运行时异常,常见的异常如下:NullPoin...

  • 异常

    几种常见的异常:1.常见的异常现象: 空指针异常类:NullPointerException 类型强制转换异常:C...

  • java 异常

    异常分类 java中常见的运行时异常 NullPointerException - 空指针引用异常 ClassCa...

  • Java中的异常

    一、异常分类 Error:Error类以及他的子类的实例,代表了JVM本身的错误 运行时异常:除Error和运行时...

  • java面试细节

    1)常见的几种异常 空指针异常NullPointerException 类强制转换异常ClassCastExcep...

  • java异常总结

    提示: 几种常见异常ArithmeticException数学算数错误异常ArrayIndexOutOfBound...

  • 04-Java异常面试题(7题)

    1、Java中异常分为哪两种? 编译时异常 运行时异常 2、异常的处理机制有几种? 异常捕捉:try…catch…...

  • Python 算法教程 笔记

    更新中。。。 第二章 2.2.2 交通规则 几种常见的渐近运行时间实例 2.2.4 三种重要情况 这里有一个 el...

  • JAVA面试题整理之—基础篇第二部

    26、列出一些你常见的运行时异常? 答:- ArithmeticException(算术异常) - ClassCa...

网友评论

      本文标题:几种常见的运行时异常实例

      本文链接:https://www.haomeiwen.com/subject/sgfwsqtx.html