常见的异常总总结

  1. (Exception)不合法的线程状态异常:Exception in thread "main" java.lang.IllegalThreadStateException
    1. package com.model.jvm;
      
      public class Thread01 {
          public static void main(String[] args) {
              //java.lang.IllegalThreadStateException , 不合法线程的状态异常
              Thread thread=new Thread();
              thread.start();
              thread.start();
      
          }
      }
  2.  (Error)栈内存溢出异常(SOF):Exception in thread "main" java.lang.StackOverflowError

    1. package com.model.jvm;
      
      
      public class JvmNote {
          public static void m1(){
              System.out.println("2222222222222");
              System.out.println("*******m1******");
              m1();
              System.out.println("333333333333");
          }
          public static void main(String[] args) {
              System.out.println("11111111111");
              m1();
              System.out.println("4444444444");
          }
      }
  3. (Error)堆内存溢出错误:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

    1. package com.model.jvm;
      
      public class Test03 {
          public static void main(String[] args) {
      //        Runtime.getRuntime(): -> JVM运行时数据区的对象
              System.out.println(Runtime.getRuntime().availableProcessors());
              Long maxMemory=Runtime.getRuntime().maxMemory();  //返回java虚拟机的试图使用的最大内存量
              Long totalMemory=Runtime.getRuntime().totalMemory(); //放回虚拟机的总内存量
              System.out.println("-Xmx:MAX_MEMORY="+maxMemory+"(字节);"+(maxMemory/(double)1024/1024)+"MB");
              System.out.println("-Xms:TOTAL_MEMORY="+totalMemory+"(字节);"+(totalMemory/(double)1024/1024)+"MB");
      
          }
      }
  4. 超出线程连接池最大容纳的线程数量:拒绝执行异常(java.util.concurrent.RejectedExecutionException)
  5. java:编译失败:内部java编译器错误:java: Compilation failed: internal java compiler error 

    1. 解决办法:  

  6. 迭代器next()超出迭代范围:(Exception in thread "main" java.util.NoSuchElementException)

  7. HashTable加入null值就会抛出控制正异常:(hashtable不能加入null,key和value都不能保存null)

    Exception in thread "main" java.lang.NullPointerException
    at java.util.Hashtable.put(Hashtable.java:465)
    at com.model.map.hashtable.HashTableDemo01.main(HashTableDemo01.java:14)

  8. TreeSet创建时没有传入comparator匿名内部类对象,且添加的元素没有实现Comparable接口就会抛出异常:

    Exception in thread "main" java.lang.ClassCastException: com.model.work.Person cannot be cast to java.lang.Comparable
    at java.util.TreeMap.compare(TreeMap.java:1294)
    at java.util.TreeMap.put(TreeMap.java:538)
    at java.util.TreeSet.add(TreeSet.java:255)
    at com.model.work.TestDmo05.main(TestDmo05.java:13)

    1.  

       

       

       

       

       

       

       

         

       

       

        
posted @ 2021-06-05 16:09  张紫韩  阅读(130)  评论(0编辑  收藏  举报