throw exception和 try catch的区别


1. 区别:

throw exception直接终止方法,或者退出循环。

try catch还要继续处理方法余下的逻辑,或者不结束循环。

try catch 不中断,throw exception中断


2.代码

(1)try catch 不中断

public class ExceptionDemo {
	
	public static void main(String[] args) {
       print();
	}
	
	private static void print(){
		int index = 0;
		while (true){
			
			try {
				Thread.sleep(200);
				++index;
				if (index == 30 || index == 50){
				   throw new Exception();	
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("Exception");
			}
			System.out.println("index = "+index);
			
			
		}
	}

}


运行结果:

index = 24
index = 25
index = 26
index = 27
index = 28
index = 29
Exception
index = 30
java.lang.Exception
at com.exception.ExceptionDemo.print(ExceptionDemo.java:19)
at com.exception.ExceptionDemo.main(ExceptionDemo.java:8)
index = 31
...
index = 48
index = 49
java.lang.Exception
at com.exception.ExceptionDemo.print(ExceptionDemo.java:19)Exception
at com.exception.ExceptionDemo.main(ExceptionDemo.java:8)


index = 50
index = 51


(2)throw exception 中断


public class ExceptionDemo {
	
	public static void main(String[] args) {
		try {
			print();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("IOException");
		}
	}
	
	private static void print() throws IOException{
		int index = 0;
		while (true){
			++index;
			if (index == 30 || index == 50){
			   throw new IOException();	
			}
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("index = "+index);
			
		}
	}

}

index = 1
index = 2
index = 3
index = 4
index = 5
index = 6
index = 7
index = 8
...
index = 28
index = 29
java.io.IOException
IOException
at com.exception.ExceptionDemo.print(ExceptionDemo.java:22)
at com.exception.ExceptionDemo.main(ExceptionDemo.java:10)

try catchthrow是Java中用于异常处理的关键字。 引用和引用提到了 throws 关键字,它用来声明一个方法可能会抛出一个异常。throws并不是抛出一个实际的 Exception,而是一个异常声明,用来提高程序的健壮性。如果方法确实会抛出异常,编译器会强制要求加上 throws 异常声明。 引用提到了 trycatch 关键字。try 用来包裹需要检测异常的代码块,如果在 try 块中发生异常,那么紧接着的 catch 块会捕获该异常,并执行相应的处理操作。catch 中的代码会在捕获到异常后执行,而 try 块中异常发生后的代码不会执行。如果 catch 块中没有 throw 抛出错误,那么 try catch 外面的代码会继续执行。 综上所述,try catch 是用来处理异常的结构,try 块中的代码可能会抛出异常,如果抛出了异常,catch 块会捕获并处理异常。throws 关键字用于声明方法可能会抛出的异常。这些关键字在 Java 中可以提高程序的健壮性和错误处理能力。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [try catchthrow区别](https://blog.csdn.net/Assassin660/article/details/113269975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Java 中 try-catch,throwthrows的使用](https://blog.csdn.net/daxiong0816/article/details/121868890)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [错误处理:try/catchthrow](https://blog.csdn.net/weixin_50576800/article/details/119982423)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值