Java异常--基本概念try...catch...finally
2013-10-09 10:25
204 查看
1、异常:
2、异常处理格式:
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("********** 计算结束 ***********") ; } };
3、异常类的继承结构
4、异常的处理机制
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("********** 计算结束 ***********") ; } };
5、总结
相关文章推荐
- java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
- java的异常机制五个关键字:throw try catch finally throw
- 十六 Java异常,try...catch...finally执行流程
- JAVA 语言如何进行异常处理,关键字:throws,throw,try,catch,finally 分别代表什么意义?在 try 块中可以抛出异常吗?
- java 异常捕捉 ( try catch finally )
- java异常处理(Try Catch Finally)
- Java异常处理中try,catch,finally的各种组合用法
- java 异常捕捉 ( try catch finally )
- Java语言中进行异常处理的关键字具体含义:throws、throw、try、catch、finally
- [CareerCup] 14.2 Try-catch-finally Java中的异常处理
- JAVA基础再回首(十九)——异常的概述、Try…Catch、多异常处理、Throws、throw、finally、自定义异常及异常的注意事项
- Java 中使用 try-catch-finally处理异常
- Java异常处理中try,catch,finally的各种组合用法
- Android学习笔记三十一:Java异常处理中try,catch,finally的各种组合用法
- java 进阶(1)--异常处理:try……catch,Throw处理异常和finally的用法
- java:异常处理机制 try-catch-throw-finally
- 基础知识《十》java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- [java][37]异常4-try-catch-finally练习
- java异常处理:try、catch、throw、throws和finally