第九天以上天数学习参见于狂神

时间:2021-07-16
本文章向大家介绍第九天以上天数学习参见于狂神,主要包括第九天以上天数学习参见于狂神使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
异常机制处理
Error
  • Error类对象由Java虚拟机生成
  • outodmemoryerror一般这时的jvm会选择线程终止
  • 当类定义、链接错误时,这些错误不可查
Exception
  • 运行异常
    • 数组下标越界ArraylndexOutOfBoundsExcepyion
    • 空指针异常NullPointerException
    • 算术异常ArithmeticException
    • 丢失资源MissingResourceException
    • 找不到类ClassNotFoundException
  • 这些异常是由逻辑错误引起的
  • error比exception更严重,往往不可处理
抛出异常
处理异常
异常处理的五个关键字:
  • try、catch、finally、throws
try{//try监控区域,错误代码}
catch(报错){ System.out.println("程序出现异常"); }
finally{ //处理善后工作
System.out.println("Finally"); }
if(条件){ throw new 异常名(); }
可以自定义异常
 
import java.io.*;
import java.util.*;
class ListOfNumbers {     
 private ArrayList list;
    private static final int size = 10;
    public ListOfNumbers () {
        list = new ArrayList(size);
        for (int i = 0; i < size; i++)
            list.add(new Integer(i));
    }
        public void writeList() {
        PrintWriter out = null;
        try {
            System.out.println("Entering try statement");
            out = new PrintWriter(new FileWriter("OutFile.txt"));
                    for (int i = 0; i <size; i++){
                out.println("Value at: " + i + " = " + list.get(i));
            }
        }
catch (ArrayIndexOutOfBoundsException e) { //处理数组越界异常。
            System.err.println("Caught ArrayIndexOutOfBoundsException: " +  e.getMessage());
        }
catch (IOException e) {  //处理I/O异常。
            System.err.println("Caught IOException: " + e.getMessage());
        } finally {  //最后清理。
            if (out != null) {
                System.out.println("Closing PrintWriter");
                out.close();
            } else {
                System.out.println("PrintWriter not open");
            }
          }
      }
}
public class TestListOfNumbers {
  public static void main(String[] args) {
    ListOfNumbers list = new ListOfNumbers();
    list.writeList();
  }
}

原文地址:https://www.cnblogs.com/yitiaofenggandeyv/p/15018973.html