package com.it;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test01 {
public static void main(String[] args) {
test03();
}
public static void test01(){
System.out.println("111");
int a = 1/0;
System.out.println("222");
}
public static void test02(){
System.out.println("111");
// 快捷键alt+command+t
try {
int a = 1/0;
} catch (Exception e) {
// throw new RuntimeException(e);
e.printStackTrace();
}
System.out.println("222");
}
/**
* RuntimeException需要程序员做逻辑处理
*/
public static void test03(){
int a =1;
int b = 0;
// ArithmeticException by Zero
// System.out.println(a/b);
if (b!=0){
System.out.println(a/b);
}
// 空指针异常 NullPointerException
String str = null;
if(str!=null){
System.out.println(str.charAt(3));
}
// 数字格式化异常 NumberFormatException
String str1 = "abc123";
// System.out.println(Integer.parseInt(str1));
Pattern p = Pattern.compile("^\\d+$");
Matcher m = p.matcher(str1);
if(m.matches()){
System.out.println(Integer.parseInt(str1));
}else{
System.out.println("数字格式不对");
}
// 数组下标越界 ArrayIndexOutOfBoundsException
int[] arr = new int[5];
int f = 3;
if (f>=0&&f<arr.length){
System.out.println(arr[f]);
}
// 类型转化异常 ClassCastException
Animal A = new Dog();
if (A instanceof Cat){
Cat C = (Cat)A;
}
}
}
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
标签:Java,int,System,static,println,机制,异常,public,out
From: https://blog.csdn.net/m0_62448751/article/details/137515773