首页 > 编程语言 > 详细

Java异常处理-异常体系结构

时间:2020-07-28 01:29:52      阅读:86      评论:0      收藏:0      [点我收藏+]
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 一、异常体系结构
 * 1.java.lang.Throwable
 *         ---java.lang.Error:一般不编写针对性的代码进行处理
 *         ---java.lang.Exception:可以进行异常处理
 *                ---编译时异常(checked)
 *                       ---IOException
 *                              ---FileNotFoundException
 *                       ---ClassNotFoundException
 *                ---运行时异常(unchecked,RuntimeException)
 *                      ---NullPointerException
 *                      ---ArrayIndexOutOfBoundsException
 *                      ---ClassCastException
 *                      ---NumberFormatException
 *                      ---InputMismatchException
 *                      ---ArithmeticException
 *
 * @author orz
 */
public class ExceptionTest {

    public static void main(String[] args) {

        //编译时异常
        /*File file=new File("hello.txt");
        FileInputStream fis=new FileInputStream(file);
        int data=fis.read();
        while (data!=-1)
        {
            System.out.println((char)data);
            data=fis.read();

        }
        fis.close();
        */


        //*****************************************//
        //运行时异常

        //空指针异常NullPointerException
        /*int [] arr=null;
        System.out.println(arr[3]);*/

        /*String str="abc";
        str=null;
        System.out.println(str.charAt(0));*/

        //数组角标越界ArrayIndexOutOfBoundsException
        /*int [] arr=new int[10];
        System.out.println(arr[11]);*/

        //StringIndexOutOfBoundsException
        /*String str="abc";
        System.out.println(str.charAt(4));*/

        //ClassCastException
       /* Object obj=new Date();
        String str=(String)obj;*/


       //NumberFormatException
        /*String str="123";
        str="abc";
        int num=Integer.parseInt(str);
        System.out.println(num);*/

        //InputMismatchException
        /*Scanner scanner=new Scanner(System.in);
        int score=scanner.nextInt();
        System.out.println(score);*/

        //ArithmeticException
        /*int a=10;
        int b=0;
        System.out.println(a/b);
        scanner.close();*/

        //

    }
}

 

Java异常处理-异常体系结构

原文:https://www.cnblogs.com/orzjiangxiaoyu/p/13388110.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!