1.主动抛出异常
public class Student {
private String name;
private int age;

public Student(String name, int age) {
    if (age<0 || age>120){
        throw new RuntimeException(); ........->作为一种特殊的返回值,来抛出异常
    }
    this.name = name;
    this.age = age;
}
-----------
 new Student("haha",180);
 ---
 Exception in thread "main" java.lang.RuntimeException
at a02.Student.<init>(Student.java:9)
at a02.FunctionDemo5.main(FunctionDemo5.java:20)
---------------------------------------------------------------------
public class ExceptionDemo1 {
public static void main(String[] args) {
    /*
    自己处理异常(捕获异常)
    格式:
    try{
        可能出现异常的代码
        }catch(异常类名 变量名){
            异常的处理代码
            }
     */
    int []arr={1,2,3};
    try{
        System.out.println(arr[3]);
    }catch(ArrayIndexOutOfBoundsException e){
        System.out.println("索引越界");
    }

    System.out.println("看看你的后面");
}

}


------------------------------- ------------------------------------- ```java 捕获异常:

package a03;

public class ExceptionDemo2 {
public static void main(String[] args) {
/*
public String getMessage() 获取异常信息,原因(提示给用户的时候,就提示错误原因)
public String toString() 获取异常类名和异常信息,原因
public void printStackTrace() 打印异常的跟踪栈信息并输出到控制台
/
int[]arr={1,2,3};
try {
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
//System.out.println(e.getMessage());//Index 3 out of bounds for length 3
//System.out.println(e.toString());//java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
e.printStackTrace();//会打印出异常的全部信息,并且不会结束虚拟机
/

//java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3at a03.ExceptionDemo2.main(ExceptionDemo2.java:12)
*/
}

    System.err.println("看看你的后面呢");
    System.out.println("你再看看你的后面呢");
    System.err.println("你再看看你的后面呢");
    System.out.println("你再看看你的后面呢");
}

}

自定义异常
public class NameFormatException extends RuntimeException{
//自定义异常
//要见名知意
//要继承
//alt+insert选前两个构造方法
public NameFormatException() {
}

public NameFormatException(String message) {
    super(message);
}

}