方式 finall 进行 什么是 区别 finally exti 目的
一、 异常
1、 什么是异常?
java 程序在运行过程中出现的意外情况
2、 java 中如何进行异常处理?
java 中的异常处理机制
try、catch、finally、throw、throws
try{
// 有可能出现异常的代码
}catch(异常类型){
// 对出现的异常进行捕获和处理
return;
//System.exit(1);// finally 语句块将不执行
}finally{
// 不管程序是否发生异常,都要执行的代码
}
try…catch…finally 一共有 3 种组合方式
try…catch…(catch 可以有多种,但要注意先子类后父类的顺序)
try…catch…finally…
try…finally….
- public class Test3 {
- private staticLogger logger=Logger.getLogger(Test3.class.getName());
- public static void main(String [] args){
- Scanner input=newScanner(System.in);
- System.out.print("请输入被除数:");
- intnum1=input.nextInt();
- System.out.println("");
- System.out.print("请输入除数:");
- intnum2=input.nextInt();
- try{
- System.out.println(num1/num2);
- System.out.println("感谢使用本程序!");
- }catch(InputMismatchException e){
- logger.error("出现错误!除数和被除数必须为整数!",e);
- }catch(ArithmeticException e){
- logger.error(e.getMessage());
- }catch(Exception e){
- logger.error(e.getMessage());
- }finally {
- System.out.println("欢饮您使用本程序!");
- }
- }
- }
- 示例
3、 常见的异常类型?表 6-1
4、 throw 和 throws2 个关键字都是用于抛出异常
区别有 3 点,P149 页
作用不同
位置不同
内容不同,
二、 log4j:1 款开源的日志记录工具
使用步骤:
1、 在项目中添加 log4j .jar 文件
2、 创建 log4j.propterties 文件【保存日志信息的相关设置】
3、 编写日志的配置信息【输出级别、目的地、格式】
4、 在程序中,使用 log4j 记录日志(sql 日志、异常日志、业务日志等 3 种类型的日志信息)
java 面向对象第六章
来源: http://www.bubuko.com/infodetail-2121796.html