Java-异常(一)-异常的概述和常见异常的举例

b站视频

124-异常处理-异常的概述与常见异常的举例_哔哩哔哩_bilibili

目录

b站视频

5.1 异常概念

5.2 Error 示例代码

5.3 Exception异常划分

❓面试题:常见的异常有哪些?举例说明


5.1 异常概念

在使用计算机语言进行项目开发的过程中,即使程序员把代码写的尽善尽美,在系统的运行过程中仍然会遇到一些问题,因为很多问题不是靠代码能够避免的,比如:客户输入数据的格式,读取文件是否存在,网络是否始终保持通畅等等。

程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常进行,这就是异常(开发过程中的语法错误和逻辑错误不是异常)

异常指的并不是语法错误,语法错了,编译不通过,不会产生字节码文件,根本不能运行.

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

  • Error: Java 虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。比如:StackOverflowError(栈溢出)OOM(内存溢出)。一般不编写针对性的代码进行处理。

  • Exception:其他因编程错误或者偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。

      例如:

      对于这些错误,一般有两种解决办法:

    • 空指针 访问

    • 试图读取不存在的文件

    • 网络连接中断

    • 数组角标越界

    • 一是遇到错误就终止程序的运行

    • 另一种办法是由程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理。

5.2 Error 示例代码

1. 栈溢出:java.lang.StackOverflowError

原因:函数调用栈太深了,注意代码中是否有了循环调用方法而无法退出的情况

        StackOverflowError,是一个java中常出现的错误,在jvm运行时的数据区域中有一个java虚拟机栈,当执行java方法时会进行压栈弹栈的操作。

        在栈中保存局部变量,操作数栈,方法出口等等,jvm规定了栈的最大深度,当执行时栈的深度大于了规定的深度,就会抛出StackOverflowError错误。

package Day1109;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;

public class TestDemo02 {
    public static void main(Stringargs){
        main(args);
    }
}

  结果:

Java-异常(一)-异常的概述和常见异常的举例_第1张图片

 2. 堆溢出:java.lang.OutOfMemoryError

原因:java中所有的对象都存储在堆中,通常如果jvm无法再分配新的内存,内存耗尽,垃圾回收无法及时回收内存,就会抛出OutOfMemoryError.

package Day1109;

public class TestDemo02 {
    public static void main(Stringargs) {
        Integerarr = new Integer[1024 1024 1024 1024];
    }
}

5.3 Exception异常划分

我们平常说的异常就是指Exception,因为这类异常一旦出现,我们就要对代码进行更正,修复程序。

异常(Exception)的分类:根据在编译时期还是运行时期去检查异常?

  • 编译时异常:

    • 是指编译器要求必须处置的异常。即程序在运行时由于外界因素造成的一般性异常。编译器要求Java程序必须捕获或声明所有编译时异常。

    • 对于这类异常,如果程序不处理,可能会带来意想不到的结果。

  • 运行时异常:

    • 是指编译器不要求强制处置的异常。一般是指编程时的逻辑错误,是程序员应该积极避免其出现的异常。java.lang.RuntimeException类及它的子类都是运行时异常。

    • 对于这类异常,可以不作处理,因为这类异常很普遍,若全处理可能会影响程序的可读性和运行效率。

Java-异常(一)-异常的概述和常见异常的举例_第2张图片

❓面试题:常见的异常有哪些?举例说明

运行时异常(RuntimeException)举例

  1. ArrayIndexOutOfBoundsException 数组下标越界异常

import java.util.Date;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args)  throws Exception{
        int[] a=new int[10];
        System.out.println(a[10]);
    }
}

结果:

 2. NullPointerException 空指针异常

import java.util.Date;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args)  throws Exception{
        //当你让字符串指向null时,并试图调用String类的一些方法,就会引发空指针异常
        String str="Hello";
        System.out.println(str.toString());
        str=null;
        System.out.println(str.toString());
    }
}

 结果:

3. ClassCastException 类型转换异常

import java.util.Date;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args)  throws Exception{
        //当年定义了一个Object类型的字符串,并试图强制转换成Interger会引发类型转换异常
        Object str="abc";
        Integer in1=(Integer) str;
       // Integer in=Integer.parseInt(str);



    }
}

结果:

4. NumberFormatException 数字格式异常

通常是由字符串转换成数字时引起的

import java.util.Date;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args)  throws Exception{
        //当定义了一个Object类型的字符串,将其强制转换成字符串类型,这就是可以的,
        //但试图将字符串类型通过Integer.parseInt方法去进行转换,就会引发数字格式异常
        Object str="abc";
        //Integer in1=(Integer) str;
        String str1= (String) str;
        System.out.println(str1);
        Integer in=Integer.parseInt(str1);



    }
}

5. InputMismatchException 输入不匹配异常

package day1124;

import java.util.Scanner;

public class DemoTest01 {
    public static void main(Stringargs) {
        Scanner sc=new Scanner(System.in);
        int in=sc.nextInt();
        System.out.println(in);
    }
}

结果:

Java-异常(一)-异常的概述和常见异常的举例_第3张图片

 6. ArithmeticException 算数异常

package Day1109;

public class TestDemo02 {
    public static void main(Stringargs) {

        int a=10;
        int b=0;
        System.out.println(a/b);
    }
}

编译时异常

1. ClassNotFoundException 类找不到异常

Java支持使用Class.forName方法来动态地加载类,任意一个类的类名如果被作为参数传递给这个方法都将导致该类被加载到JVM内存中,如果这个类在类路径中没有被找到,那么此时就会在运行时抛出ClassNotFoundException异常。

package day1124;

import day1103.Student;

import java.util.Scanner;

public class DemoTest01 {
    public static void main(String[] args) {
        Class clz=Class.forName("java.lang.String");


    }
}

结果:

Java-异常(一)-异常的概述和常见异常的举例_第4张图片

 2. FileNotFoundException

原因:文件找不到异常通常是两种情况:1)系统找不到指定的路径 2)拒绝访问(指定的是目录时,就会报拒绝访问异常)

package Day1109;

import java.io.FileInputStream;

public class TestDemo02 {
    public static void main(String[] args) {

        FileInputStream fis=new FileInputStream("a.txt");
    }
}

 结果

Java-异常(一)-异常的概述和常见异常的举例_第5张图片

3. IOException

package day1124;

import day1103.Student;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DemoTest01 {
    public static void main(Stringargs) throws FileNotFoundException {
        FileInputStream fis=new FileInputStream("C:\\Users\\35014\\Idea46\\src\\day1120\\a.txt");
        //汉字是两个字节,read函数是一个字节一个字节的读
        int data=fis.read();
        System.out.println(data);


    }
}

 结果:

Java-异常(一)-异常的概述和常见异常的举例_第6张图片

 

 

你可能感兴趣的:(Java,SE知识点总结,java,开发语言)