package Basic;
import java.io.IOException;
import java.sql.SQLException;
/**
* 异常处理注意点:
* 1.在写程序时,对可能会出现异常的部分通常要用try{...}catch{...}去捕捉它并对它进行处理;
* 2.用try{...}catch{...}捕捉了异常之后一定要对在catch{...}中对其进行处理,那怕是最简单的一句输出语句,或栈输入e.printStackTrace();
* 3.如果是捕捉IO输入输出流中的异常,一定要在try{...}catch{...}后加finally{...}把输入输出流关闭;
* 4.如果在函数体内用throw抛出了某种异常,最好要在函数名中加throws抛异常声明,然后交给调用它的上层函数进行处理
* @author jio.zhou
*
*/
public class Trycatch {
/**
* 基本情况
* 先执行try中的代码块
* 有异常:catch--finally
* 无异常:finally
* @return
*/
public static String testTry1(){
int x = 1;
try{
//int a = 1/0;//抛出异常
x++;
System.out.println("try执行:" + x);
}catch (Exception e){
x++;
e.printStackTrace();
System.out.println("catch执行:" + x);
}finally {
x++;
System.out.println("finally执行:" + x);
}
return "最后结果:" + x ;
}
/**
* 嵌套情况
* 先执行外层try中的代码块
* 有异常:外层异常:外层catch--外层finally 内层异常:内层catch--内层finally--外层finally
* 无异常:内层try--内层finally--外层finally
* @return
*/
public static String testTry2(){
int x = 1;
try{
//int a = 1/0;//外层抛出异常
x++;
System.out.println("外层try执行:" + x);
try {
//int a = 1/0;//内层抛出异常
x++;
System.out.println("内层try执行:" + x);
}catch(Exception e) {
x++;
System.out.println("内层catch执行:" + x);
}finally {
x++;
System.out.println("内层finally执行:" + x);
}
}catch (Exception e){
e.printStackTrace();
x++;
System.out.println("外层catch执行:" + x);
}finally {
x++;
System.out.println("外层finally执行:" + x);
}
return "最后结果:" + x ;
}
/**
* 循环情况
* 循环中包含try...catch...捕获异常后继续循环,continue放入catch中
* @return
*/
public static String testTry3(){
int x = 1;
for(int i = 0 ; i<10; i++) {
try {
x = i;
System.out.println("try执行:" + x);
if(x==5) {
int a = 1/0; //循环6次抛出异常
}
}catch(Exception e) {
e.printStackTrace();
System.out.println("catch执行:" + x);
continue;
}finally {
System.out.println("finally执行:" + x);
}
//finally块通常是可选的。
//无论异常是否发生,异常是否匹配被处理,finally都会执行。
//一个try至少要有一个catch块,否则, 至少要有1个finally块。但是finally不是用来处理异常的,finally不会捕获异常。
//finally主要做一些清理工作,如流的关闭,数据库连接的关闭等,最好不要用于做其它工作。
}
return "最后结果:" + x ;
}
/**
* 一次性捕获多种异常
* 多次捕获顺序异常时,一定要把异常范围小的放在前面,范围大的放在后面,Exception这个异常的根类一定要放在最后一个catch里面,如果放在前面或者中间,任何异常都会和Exception匹配的,就会报已捕获到...异常的错误
* @return
*/
public static void testTry4(String a){
try {
if(a=="io") {
throw new IOException("IOException");
}else if(a=="sql") {
throw new SQLException ("SQLException");
}
}catch(IOException | SQLException e) {
e.printStackTrace();
}
}
/**
* 自定义异常。throw主动抛出异常,new一个异常类型,抛出异常打印string。抛出异常直接进行了return
* throw语句用在方法体内,表示抛出异常,由方法体内的语句处理。是具体向外抛异常的动作,所以它是抛出一个异常实例。
* throws语句用在方法声明后面,表示再抛出异常,由该方法的调用者来处理。主要是声明这个方法会抛出这种类型的异常,使它的调用者知道要捕获这个异常。
* throws可以跟多个异常类型,使用throws的方法,在被调用时都需要去处理这个异常,比如采用try..catch
* @return
*/
public static String testTry5() throws Exception,ArithmeticException{
//throw new ArithmeticException("divide by 0");//主动产生一个除以0的异常
//throw new NullPointerException("null point");//主动产生空指针异常
throw new Exception("normal exception");//主动产生exception
}
public static void main(String[] args){
String result;
try {
result = testTry5();
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}