异常综合练习

异常综合练习


下面示例经常出现在面试过程中,需要了解运行过程和执行结果

package com.tedu.exception;
/**
 * 异常综合练习
 * 
 * @author Wildmess
 *
 *不管怎样都会执行的语句
 *finally(关键字,常量),fianlly(异常中的块),finalize(Object类中的方法)
 */
public class FinallyDemo4 {
	public static void main(String[] args) {
		System.out.println(test(null));
		System.out.println(test(""));
		System.out.println(test("a"));//97
		System.out.println(test(null) + "," + test("") + "," +test("a"));
	}
	public static int test(String str) {
		try {
			System.out.println("调用了test:" + str);
			return str.charAt(0);
		} catch (NullPointerException e) {
			System.out.println("出现了空指针!");
			return 1;
		} catch(StringIndexOutOfBoundsException e) {
			System.out.println("出现了字符越界!");
			return 2;
		} catch (Exception e) {
			System.out.println("未知错误!");
			return 3;
		} finally {
			System.out.println("执行了finally");
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值