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("除數(shù)不能為0");
} finally {
System.out.println("必須要執(zhí)行的步驟,一定會執(zhí)行");
}
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("數(shù)據(jù)格式不對");
} catch (AbstractMethodError e) {
System.out.println("算術(shù)異常");
}/
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("發(fā)現(xiàn)了異常全款,無法處理,交給上級處理");
throw new Exception("發(fā)現(xiàn)了異常情況,無法處理,交給上級處理");
}
}
}
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("發(fā)現(xiàn)了異常全款准夷,無法處理,交給上級處理");
throw new SexException("發(fā)現(xiàn)了異常情況确丢,無法處理霎箍,交給上級處理");
}
}
}
public class SexException extends Exception {
public SexException(){
}
public SexException(String message) {
super(message);
System.out.println("自定義的異常處理類.抓住了異常,暫時不處理");
}
}