当前位置:首页 > Java技术 > 异常之Error和Exception(异常机制)

异常之Error和Exception(异常机制)

什么是异常:

实际工作中,遇到的情况不可能是非常完美的。比如:你写的某个模块,用户输入了不一定符合你的要求、你的程序要打开某个文件,这个文件可能不存在或者文件格式不对,你要读取数据库的数据,数据可能是空的等!我们程序在运行着,内存或硬盘满了。

软件程序运行过程中,非常可能遇到以上这些异常问题,那怎么让我们写的程序做出合理的处理而不至于崩溃。

异常分类:

  检查性异常:最具代表的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的,例如要打开一个不存在的文件时,一个异常就发生,这些异常在编译时不能被简单地省略。

  运行时异常:运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时被忽略。

  错误:错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略,例如,当栈溢出时,一个错误就发生了,它们在编译也检查不到的。

体系:

/*
* 一、异常的体系结构
* 1、java.lang.Throwable
* ----java.lang .Error:一般不编写针对性的代码进行处理
* ----java.lang.Exception:可以进行异常处理
* ---编译时异常
* ----IOExcptio
* ---FileNotFoundExcption
* ----ClassNotExcptio
*
*
* ---运行时异常
* ---NullPoinerExcption
* ---ArrayIndexOutfBoundsExcption
* ---ClassCastExcption
* ---NumberFormatExcption
* ---InptMismatchExcption
* ---ArithmeticExcption
*
* 面试题:常见的异常有那些?举例说明
*/


异常举例:

package 异常;

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

import org.junit.Test;


import com.sun.org.apache.bcel.internal.classfile.Field;
import com.sun.org.apache.bcel.internal.classfile.InnerClass;
import com.sun.org.apache.bcel.internal.generic.NEW;

/*
 * 一、异常的体系结构
 * 1、java.lang.Throwable
 *                 ----java.lang.Error:一般不编写针对性的代码进行处理
 *                 ----java.lang.Exception:可以进行异常处理
 *                         ---编译时异常
 *                             ----IOExcptio
 *                                     ---FileNotFoundExcption
 *                             ----ClassNotExcptio     
 *                     
 * 
 *                                 ---运行时异常(unchecked,RuntimeException )
 *                                     ---NullPoinerExcption
 *                                     ---ArrayIndexOutfBoundsExcption
 *                                     ---ClassCastExcption
 *                                     ---NumberFormatExcption
 *                                     ---InptMismatchExcption
 *                                     ---ArithmeticExcption
 *         
 * 面试题:常见的异常有那些?举例说明
 */
public class ExceptionTest {
    //*********编译时异常
//    @Test
//    public void test7() {
//            File file=new File("hee.txt");
//            FileInputStream lw=new FileInputStream(file);
//            int ar=file.read();
//            while(ar!=-1) {
//                System.out.println((char)ar);
//                ar=file.read();
//            }
//            file.close();
//    }
    
    
//****************以下是运行时异常
    //ArithmeticExcption
    @Test
    public void test6() {
        int a=10;
        int b=0;
        System.out.println(a/b);
    }
    
    //InptMismatchExcption
    @Test
    public void test5() {
        Scanner scanner=new Scanner(System.in);
        int sr=scanner.nextInt();
        System.out.println(sr);
        scanner.close();
    }
    
//NumberFormatExcption
    @Test
    public void test4() {
        String str="123";
        str="abc";
        System.out.println(Integer.parseInt(str));
    }
    
    //ClassCastExcption
    
    @Test
    public void test3() {
        Object obj=new Date();
        String str=(String)obj;
    }
    //ArrayIndexOutfBoundsExcption
    @Test
    public void test2() {
//        int []arr=new int [10];
//        System.out.println(arr[10]);
//        String str="abc";
//        System.out.println(str.charAt(3));
    }
    
    
//---NullPoinerExcption
//@Test
//public void test1() {
////    int []arr=null;
////    System.out.println(arr[3]);
//String str="abc";
//str=null;
//System.out.println(str.charAt(0));
//}
}

 

作者:hollg
来源链接:https://www.cnblogs.com/huxingchen/p/16117543.html


版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/java/68840.html

标签: 异常Exception
分享给朋友: