public static void main(String[] args) {
main(args);
//递归调用本身的main方法
}
public static void main(String[] args) {
int []i =new int[1024*1024*1024];
}
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