public class main {
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("哈哈哈哈哈哈");
}
}
import java.util.InputMismatchException;
import java.util.Scanner;
public class main2 {
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[2] = j;
System.out.println(a[0] + a[1]);
} catch (IndexOutOfBoundsException e) {
System.out.println("越界异常");
} catch (InputMismatchException e) {
System.out.println("数据格式不对");
} catch (AbstractMethodError e) {
System.out.println("算术异常");
}/
int [] a = new int[2];
Scanner scanner = new Scanner(System.in);
try {
int i = scanner.nextInt();
int j = scanner.nextInt();
a[0] = i;
a[2] = j;
System.out.println(a[0] / a[1]);
}catch (IndexOutOfBoundsException | InputMismatchException | ArithmeticException e ){
System.out.println("其中的一个错误");
}
}
}
public class main3 {
public static void main(String [] args) throws Exception {
/ try{
setSex("afaqfae");
}catch (Exception e ){
System.out.println("上级处理下级抛出的异常");
}*/
setSex("sfwf");
}
public static void setSex(String sex ) throws Exception{
if ( ! (sex.equals("男") || sex.equals("女")) ){
System.out.println("发现了异常全款,无法处理,交给上级处理");
throw new Exception("发现了异常情况,无法处理,交给上级处理");
}
}
}
public class main4 {
public static void main(String [] args) throws SexException {
try{
setSex("afaqfae");
}catch (Exception e ){
System.out.println("上级处理下级抛出的异常");
}
}
public static void setSex(String sex ) throws SexException{
if ( ! (sex.equals("男") || sex.equals("女")) ){
//System.out.println("发现了异常全款,无法处理,交给上级处理");
throw new SexException("发现了异常情况,无法处理,交给上级处理");
}
}
}
public class SexException extends Exception {
public SexException(){
}
public SexException(String message) {
super(message);
System.out.println("自定义的异常处理类.抓住了异常,暂时不处理");
}
}