### 异常
```java
public class Demo01 {
public static void main(String[] args) {
int divisor = 100;
int dividend = 0;
try {
System.out.println(divisor/dividend);
}catch (Exception e){
//e.printStackTrace();
System.out.println("除数不能为0");
}finally {
System.out.println("必须要执行的步骤,一定会执行");
}
System.out.println("啊啊啊啊啊啊啊啊啊啊");
}
}
public class Demo02 {
public static void main(String[] args) {
/* int [] a=new int[2];
Scanner scanner=new Scanner(System.in);
try{
int i= scanner.nextInt();
int j=scanner.nextInt();
a[0]=i;
a[1]=j;
System.out.println(a[0]+a[1]);
}catch (IndexOutOfBoundsException e){
System.out.println("越界异常");
}catch (InputMismatchException e){
System.out.println("数据格式不对");
}catch (ArithmeticException e){
System.out.println("算数异常");
}*/
int [] a = new int[2];
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
int j = scanner.nextInt();
try {
a[0] = i;
a[2] = j;
System.out.println(a[0] + a[1]);
}catch (IndexOutOfBoundsException | InputMismatchException | ArithmeticException e){
System.out.println("其中一个错误");
}
}
}
public static void main(String[] args) throws Exception {
/* try {
setSex("asxxowjsmx,x");
}catch (Exception e){
System.out.println("上级处理下级抛出的异常");
}*/
setSex("sdxdfss");
}
public static void setSex(String sex) throws Exception { //声明要抛出异常
if (!(sex.equals("男") ||sex.equals("女"))){
System.out.println("发现异常情况,无法处理,交给上级处理");
throw new Exception("发现异常情况,无法处理,交给上级处理"); // 抛出异常 本级函数不处理异常,抛出给调用者
}
}
public class SexException extends Exception{
public SexException(){
}
public SexException(String message){
super(message);
//System.out.println("自定义的异常处理类,抓住了异常,暂时不处理");
}
}
public class Demo04 {
public static void main(String[] args) throws Exception {
try {
setSex("asxxowjsmx,x");
}catch (Exception e){
System.out.println("上级处理下级抛出的异常");
}
setSex("sdxdfss");
}
public static void setSex(String sex) throws SexException { //声明要抛出异常
if (!(sex.equals("男") ||sex.equals("女"))){
//System.out.println("发现异常情况,无法处理,交给上级处理");
throw new SexException("发现异常情况,无法处理,交给上级处理"); // 抛出异常 本级函数不处理异常,抛出给调用者
}
}
}