首页 > 编程语言 > 详细

Java程序在执行过程中所发生的异常事件可分为两类:Error、Exception

时间:2021-07-03 22:37:46      阅读:22      评论:0      收藏:0      [点我收藏+]

异常:在Java语言中,将程序执行中发生的不正常情况称为“异常”。 (开发过程中的语法错误和逻辑错误不是异常).Java程序在执行过程中所发生的异常事件可分为两类:Error、Exception

  • Error: Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。比如:StackOverflowErrorOOM。一般不编写针对性的代码进行处理
  • Exception: 其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。例如:
  • 空指针访问
  • 试图读取不存在的文件
  • 网络连接中断
  • 数组角标越界

Error

  • StackOverflowerError异常(栈溢出)
 public static void main(String[] args) {
    main(args);
   //递归调用本身的main方法
    }
  • OutOfMemoryError异常(堆溢出简称OOM)
 public static void main(String[] args) {
        int []i =new int[1024*1024*1024];
       }

Exception 常见的异常

package com.yuteng;


import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Scanner;

/**
 * @version 1.0
 * @author: 余腾
 * @date: 2021-07-03 16:42
 * 一、异常体系结构
 *      |----java.lang.Error: 一般不编写针对性的代码进行处理
 *      |----java.lang.Exception:可以进行异常的处理
 *         |---- 编译时异常(checked)
 *              |---- IOException
 *                 |--- FileNotFoundException
 *              |---- ClassNotFoundException
 *         |---- 运行时异常(unchecked)
 *            |---- NullPointException
 *            |---- ArrayIndexOutOfBoundsException
 *            |---- ClassCastException
 *            |---- NumberFormatException
 *            |---- InputMismatchException
 *            |---- ArithmaticException
 */
public class ExceptionTest {
    //编译时异常 写完打X了
    /**
     * FileNotFoundException
     */
    @Test
    public  void FileNotFoundException(){
        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();
    }
   //运行时异常
    /**
     * NullPointException  空指针异常
     */
    @Test
    public void NullPointException (){
        int [] i =null;
        System.out.println(i[0]);
        //字符串空指针
        // String str=null;
        //System.out.println(str.charAt(0));
    }

    /**
     * ArrayIndexOutOfBoundsException 角标越界异常
     */
    @Test
    public void ArrayIndexOutOfBoundsException(){
        int [] i= new  int[3];
        System.out.println(i[3]);
        //字符串角标越界
//        String  str="abc";
//        System.out.println(str.charAt(3));
    }
    /**
     *  ClassCastException 类型转换异常
     */
    @Test
    public void  ClassCastException(){
        Object obj=new Date();
        String  str= (String) obj;
    }
    /**
     * NumberFormatException  数据转换异常
     */
    @Test
    public void NumberFormatException(){
        String str="abc";
        int num=Integer.parseInt(str);
    }
    /**
     * InputMismatchException 数据输入异常
     */
    @Test
    public  void InputMismatchException(){
        Scanner sc=new Scanner(System.in);
        int i=sc.nextInt();
        //然后你输出abc 就会爆出异常
        sc.close();
    }
    /**
     * ArithmaticException  算术异常
     */
    @Test
    public void  ArithmaticException(){
        int a=10;
        int b=0;
        System.out.println(a / b);
    }
}

异常的解决策略

  • 遇到错误就终止程序的运行。
  • 由程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理。

异常概述与体系结构技术分享图片

Java程序在执行过程中所发生的异常事件可分为两类:Error、Exception

原文:https://www.cnblogs.com/yuteng666/p/14967347.html

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