try catch finally语法,以及throws、throw关键字

1.4.try catch finally语法
    // 方法涉及返回值时,try catch 就没法继续运行后续代码
    //想要执行后续代码,使用finally
    
    public static int test1() {
		
		System.out.println("1.开始");
		
		try {
			String name = null;
			System.out.println(name.getClass());
			
		}catch(NullPointerException ex) {
			System.out.println("处理空指针异常");
			
			//出现空指针异常时,直接返回,后续代码不会运行
			return -1;
		}
		catch(Exception ex) {
			System.out.println("处理其他异常");
		}
		
		//想要运行这这段代码,使用finally
		finally {
			
			System.out.println("2.这段代码必须运行");
		}
		
		System.out.println("3");
		return 0;
	}
    
1.5 try finally 语法 (只有捕获,没有处理)
    
   public static void test2() {

		System.out.println("1.开始");

		try {
			String name = null;
			System.out.println(name.getClass());
		} finally {

			System.out.println("2.这段代码必须运行");
		}

		System.out.println("3");
	}
    
运行结果:1.2.空指针异常

2.2 检查异常

2.检查异常:(编译时显示报错的异常)
    必须处理,否则代码无法运行
    
    当前方法暂停一秒钟
	Thread.sleep(1000); 检查异常(InterruptedException)
throws关键字 (显示抛出) 抛给上一级调用该方法的方法去处理。也可一级一级直至抛到jvm为止。
    
 public class DemoThrows {

        //讲异常抛给JVM
	public static void main(String[] args) throws InterruptedException{
	
		test2();		
	}

	//throw 将异常抛给上一级处理
	public static void test2() throws InterruptedException{
		
		//受查异常,不处理就没法运行
		Thread.sleep(1000);		
	}
 }   
 throw  直接抛出异常,后续代码不会执行。
    	语法:throw new Exception();

1.抛出运行时异常
public class DemoThrow {

	public static void main(String[] args) throws Exception {

		test1(null);
		
	}
    // 抛出运行时异常
	public static void test1(String str) {

		if (null == str) {

			// 直接抛出异常
			throw new NullPointerException();
		}
	}
}

2.抛出受查异常
public class DemoThrow {

	public static void main(String[] args) {

		try {
			test2(null); //可以往外抛也可使用try catch自己处理
		} catch (Exception e) {

			System.out.println("-------------");
		}
	}
	// 将受检异常抛走
	public static void test2(String str) throws Exception {

		if (null == str) {

			// 直接抛出受查异常
			throw new Exception();
		}
	}
}

2.3.自定义异常

 3.1 自定义运行时异常: extends RuntimeException
    
 public class Test2 {

	public static void main(String[] args) {

		test1();
	}

	public static void test1() {

		//抛出自己定义的异常
		throw new MyException("----------");
	}
}

//自己定义运行时异常
class MyException extends RuntimeException {

	public MyException() {	
	}
	//带参构造器,可以传递异常信息
	public MyException(String str) {
		super(str);	
	}
}  
-----------------------------------------------------------------------
    
3.2自定义检查异常:extends Exception(IOException)
public class Test2 {

	public static void main(String[] args) throws MyException1 {

		test2();
	}
	public static void test2() throws MyException1 {

		//抛出自己定义的受查异常
		throw new MyException1("----------");
	}	
}

//自己定义受查异常
class MyException1 extends Exception {

	public MyException1() {
	
	}
	//带参构造器,可以传递异常信息
	public MyException1(String str) {
		super(str);	
	}
}

父类子类抛出异常问题

1.方法重写的时候,如果父类没有抛出任何异常,子类不可抛出编译时异常,可以抛出运行时异常

2.父类的 方法抛出异常,子类在方法重写的时候不能抛出比父类更宽泛的编译时异常

3.子类重写方法可以随时抛出运行时异常。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值