public class ExceptionDemo01{ public static void main(String args[]){ System.out.println("********** 计算开始 ***********") ; int i = 10 ; // 定义整型变量 int j = 0 ; // 定义整型变量 int temp = i / j ; // 此处产生了异常 System.out.println("两个数字相除的结果:" + temp) ; System.out.println("********** 计算结束 ***********") ; } };
public class ExceptionDemo02{ public static void main(String args[]){ System.out.println("********** 计算开始 ***********") ; int i = 10 ; // 定义整型变量 int j = 0 ; // 定义整型变量 try{ int temp = i / j ; // 此处产生了异常 System.out.println("两个数字相除的结果:" + temp) ; System.out.println("----------------------------") ; }catch(ArithmeticException e){ System.out.println("出现异常了:" + e) ; } System.out.println("********** 计算结束 ***********") ; } };
public class ExceptionDemo03{ public static void main(String args[]){ System.out.println("********** 计算开始 ***********") ; int i = 10 ; // 定义整型变量 int j = 0 ; // 定义整型变量 try{ int temp = i / j ; // 此处产生了异常 System.out.println("两个数字相除的结果:" + temp) ; System.out.println("----------------------------") ; }catch(ArithmeticException e){ // 捕获算术异常 System.out.println("出现异常了:" + e) ; }finally{ // 作为异常的统一出口 System.out.println("不管是否出现异常,都执行此代码") ; } System.out.println("********** 计算结束 ***********") ; } };
public class ExceptionDemo04{ public static void main(String args[]){ System.out.println("********** 计算开始 ***********") ; int i = 0 ; // 定义整型变量 int j = 0 ; // 定义整型变量 try{ String str1 = args[0] ; // 接收第一个参数 String str2 = args[1] ; // 接收第二个参数 i = Integer.parseInt(str1) ; // 将第一个参数由字符串变为整型 j = Integer.parseInt(str2) ; // 将第二个参数由字符串变为整型 int temp = i / j ; // 此处产生了异常 System.out.println("两个数字相除的结果:" + temp) ; System.out.println("----------------------------") ; }catch(ArithmeticException e){ // 捕获算术异常 System.out.println("出现异常了:" + e) ; } System.out.println("********** 计算结束 ***********") ; } };
public class ExceptionDemo05{ public static void main(String args[]){ System.out.println("********** 计算开始 ***********") ; int i = 0 ; // 定义整型变量 int j = 0 ; // 定义整型变量 try{ String str1 = args[0] ; // 接收第一个参数 String str2 = args[1] ; // 接收第二个参数 i = Integer.parseInt(str1) ; // 将第一个参数由字符串变为整型 j = Integer.parseInt(str2) ; // 将第二个参数由字符串变为整型 int temp = i / j ; // 此处产生了异常 System.out.println("两个数字相除的结果:" + temp) ; System.out.println("----------------------------") ; }catch(ArithmeticException e){ // 捕获算术异常 // System.out.println("算术异常:" + e) ; e.printStackTrace() ; }catch(NumberFormatException e){ // 捕获数字转换异常 System.out.println("数字转换异常:" + e); }catch(ArrayIndexOutOfBoundsException e){ // 捕获数组越界异常 System.out.println("数组越界异常:" + e) ; } System.out.println("********** 计算结束 ***********") ; } };
public class ExceptionDemo06{ public static void main(String args[]){ System.out.println("********** 计算开始 ***********") ; int i = 0 ; // 定义整型变量 int j = 0 ; // 定义整型变量 try{ String str1 = args[0] ; // 接收第一个参数 String str2 = args[1] ; // 接收第二个参数 i = Integer.parseInt(str1) ; // 将第一个参数由字符串变为整型 j = Integer.parseInt(str2) ; // 将第二个参数由字符串变为整型 int temp = i / j ; // 此处产生了异常 System.out.println("两个数字相除的结果:" + temp) ; System.out.println("----------------------------") ; }catch(ArithmeticException e){ // 捕获算术异常 // System.out.println("算术异常:" + e) ; e.printStackTrace() ; }catch(NumberFormatException e){ // 捕获数字转换异常 System.out.println("数字转换异常:" + e); }catch(ArrayIndexOutOfBoundsException e){ // 捕获数组越界异常 System.out.println("数组越界异常:" + e) ; }catch(Exception e){ System.out.println("其他异常:" + e) ; } System.out.println("********** 计算结束 ***********") ; } };
public class ExceptionDemo07{ public static void main(String args[]){ System.out.println("********** 计算开始 ***********") ; int i = 0 ; // 定义整型变量 int j = 0 ; // 定义整型变量 try{ String str1 = args[0] ; // 接收第一个参数 String str2 = args[1] ; // 接收第二个参数 i = Integer.parseInt(str1) ; // 将第一个参数由字符串变为整型 j = Integer.parseInt(str2) ; // 将第二个参数由字符串变为整型 int temp = i / j ; // 此处产生了异常 System.out.println("两个数字相除的结果:" + temp) ; System.out.println("----------------------------") ; }catch(Exception e){ System.out.println("其他异常:" + e) ; }catch(ArithmeticException e){ // 捕获算术异常 // System.out.println("算术异常:" + e) ; e.printStackTrace() ; } System.out.println("********** 计算结束 ***********") ; } };
public class ExceptionDemo08{ public static void main(String args[]){ System.out.println("********** 计算开始 ***********") ; int i = 0 ; // 定义整型变量 int j = 0 ; // 定义整型变量 try{ String str1 = args[0] ; // 接收第一个参数 String str2 = args[1] ; // 接收第二个参数 i = Integer.parseInt(str1) ; // 将第一个参数由字符串变为整型 j = Integer.parseInt(str2) ; // 将第二个参数由字符串变为整型 int temp = i / j ; // 此处产生了异常 System.out.println("两个数字相除的结果:" + temp) ; System.out.println("----------------------------") ; }catch(Exception e){ System.out.println("其他异常:" + e) ; } System.out.println("********** 计算结束 ***********") ; } };