Rumah > Java > javaTutorial > Java基础值:异常处理关键字...

Java基础值:异常处理关键字...

怪我咯
Lepaskan: 2017-06-26 11:46:53
asal
1521 orang telah melayarinya

都说java 语言是非常健壮性 如:垃圾回收机制、内存模型、异常处理,强类型转换、跨平台,等等,使得Java语言的受到青睐。今天我们先来聊聊java的异常处理机制try catch finally throw throws,平时我们貌似小瞧了这五个关键字。开发应用系统,良好异常的处理对系统后期的开发、维护、升级、用户的体验尤其重要。

异常有个非常重要的特征,从出现异常的位置 到 最顶端的main方法 如果你一直没catch到它,最终jvm会帮你抛出异常信息,糟糕的是该线程断掉,后续代码也不再执行,从此无声无息的消失在jvm这片汪洋大海。前面我公司的一个项目前端ajax请求control做支付,由于control的catch里面抛出了一个空指针,导致前端页面卡住不动了,解决方案:由于control层一般是顶层最好catch到任何有可能出现异常的地方,其他非顶层还可以继续throw 或者throws往上抛。还有就是最好为每个ajax设置超时时间。

 

先简单介绍下throw 跟throws:

throw :在方法体内抛出一个异常,实际存在的异常对象,一般用是自己扩展的异常实例,throws是放在方法名后面,表示该方法如果出现异常 , 我不想处理或者处理不了,交给调用者处理,可以thow抛出一个运行时异常(unchecked)如ClassNotFoundException,NumberFromartException,   也可以throws或throw+try或throw+throws 处理一个checked异常如:IOExcepion,SocketException、继承exception类之类的异常, 。区别是checked异常必须处理(要么try,要么throws继续往上抛,否则编译是通不过的),而运行时异常可以不处理,一直不处理的后果是出现异常后jvm报出异常信息然后线程断掉。 throw 跟throws关键字一般情况不建议在代码中使用,推荐所有异常就地解决。知道用就行了,不做过多的讲解。

try的组合规则:1, try{}catch(){}  2,try{}catch(){}finally{}  3,try{}finally{}    ,1跟2的方式 catch可以有多个

 

朋友,吃几颗栗子:

1,无try组合  

public class CatchExecuteJustOne {
  public void methodOne(){
    System.out.println("into methodOne method");
    int one=1/0;
    System.out.println("end  methodOne method"); //不会输出  没有try组合,报错后线程已经断掉
}
  public static void main(String[] args) {
    CatchExecuteJustOneS cejo = new CatchExecuteJustOneS();
    cejo.methodOne();

    System.out.println("end  main method"); //不会输出  没有try组合 报错线程已经断掉
  }
}

输出:

Into methodOne method
Exception in thread "main" java.lang.ArithmeticException: / by zero
at priv.lilei.exception.example_1.CatchExecuteJustOneS.methodOne(CatchExecuteJustOneS.java:6)
at priv.lilei.exception.example_1.CatchExecuteJustOne.main(CatchExecuteJustOne.java:19)

 

2.1,有try组合案例1

public class CatchExecuteJustOne {
  public void methodOne(){
    System.out.println("into methodOne method");
  try{
    int one=1/0;
  }catch(Exception e){
    System.out.println("methodOne try到");
  }
  System.out.println("end  methodOne method");
}

  public static void main(String[] args) {
    CatchExecuteJustOne cejo = new CatchExecuteJustOne();
    cejo.methodOne();
    System.out.println("end  main method");
  }
}

 输出:

into methodOne method
methodOne try到
end  methodOne method
end  main method

2.2,有try组合案例2

public class CatchExecuteJustOne {
public void methodOne(){
  System.out.println("into methodOne method");
  int one=1/0;
  System.out.println("end  methodOne method"); //不会执行线程上面报错断掉直接抛出了
}

public static void main(String[] args) {
  try{
    CatchExecuteJustOne cejo = new CatchExecuteJustOne();
    cejo.methodOne();
  }catch(Exception exception){
    System.out.println("into main method catch"); //会执行 try到上面方法报的异常
  }
    System.out.println("end  main method"); //会执行  try到上面方法报的异常
 }
}

输出:

into methodOne method
into main method catch
end  main method

 

2.3,有try案例组合3    异常只会被最近捕捉到它的catch 一次。像switch case 跟if() if else(){} if()else if{} 语法一样

 

public class CatchExecuteJustOne {
public void methodOne(){
System.out.println("into methodOne method");
try{
int one=1/0;
}catch(ArithmeticException e){
System.out.println("catch 1");
}catch (Exception e) {
System.out.println("catch 2");//不会执行 已经执行了上一个catch 1
}
}

public static void main(String[] args) {
CatchExecuteJustOne cejo = new CatchExecuteJustOne();

try {
cejo.methodOne();
} catch (Exception e) {
System.out.println("man catch");//不会执行已经执行了catch 1
}

System.out.println("end main method");
}
}

输出:

into methodOne method
catch 1
end main method

2.4 有try组合案例4,  try{}finally{}组合,finally没中有返回值得时候线程会断掉,但在finally中有返回值时候线程不会断掉只是后续代码不会执行, 这种组合建议少用。

 //没返回值

public class CatchExecuteJustOne {
  public void methodOne(){  //没返回值
    System.out.println("into methodOne method");
  try{
    int one=1/0;
  }finally{
    System.out.println("into methodOne finally");
  }
    System.out.println("end  methodOne method"); //不会执行线程上面报错断掉直接抛出了
}

public static void main(String[] args) {
  CatchExecuteJustOne cejo = new CatchExecuteJustOne();
  cejo.methodOne();
  System.out.println("end  main method");//不会执行线程上面报错断掉直接抛出了
  }
}

没返回值的输出:

into methodOne method
Exception in thread "main" into methodOne finally
java.lang.ArithmeticException: / by zero
at priv.lilei.exception.example_1.CatchExecuteJustOne.methodOne(CatchExecuteJustOne.java:14)
at priv.lilei.exception.example_1.CatchExecuteJustOne.main(CatchExecuteJustOne.java:23)

有返回值:

public class CatchExecuteJustOne {
public String methodOne(){
System.out.println("into methodOne method");
try{
System.out.println("1");
int one=1/0;
System.out.println("2");//不会执行线程上面报错断掉直接抛出了
}finally{
System.out.println("into methodOne finally");//会输出
return "1";
}
}

public static void main(String[] args) {
CatchExecuteJustOne cejo = new CatchExecuteJustOne();
cejo.methodOne();
System.out.println("end  main method");//会执行 因为上面有try到并且方法有返回值
}
}

有返回值的输出:

into methodOne method
1
into methodOne finally
end  main method

 

2.5,带finally的组合  finally永远被执行,有返回值得情况在返回之前执行,  除非出现特别暴力的行为如 system.exit(0); 或者断掉了,或者内存溢出了等Error错误。

return 组合

2.5.1 下面两个案例  在没有异常 跟有异常的情况  ,在catch跟finally 中给变量再次赋值   存在差异。没有异常再次赋值失败,而有异常再次赋值成功。

 

1 没有异常的情况 

public class CatchExecuteJustOne {
public String methodOne(){
String a="";
System.out.println("into methodOne method");
try{
a="a";
return a;
}catch(ArithmeticException e){
System.out.println("catch 1");
}finally {
System.out.println(1);
a="a2";        //不报错的情况   不会赋值给a;
System.out.println(2);
}
System.out.println(3); //不会执行 上面return a方法已经返回了
return a;
}

public static void main(String[] args) {
CatchExecuteJustOne cejo = new CatchExecuteJustOne();
System.out.println(cejo.methodOne());
}
}

try中return 没有异常的情况的输出:

into methodOne method
1
2
a

2 有异常的情况

public class CatchExecuteJustOne {
public String methodOne(){
String a="";
System.out.println("into methodOne method");
try{
a="a";
int i=1/0;
return a;
}catch(ArithmeticException e){
System.out.println("catch 1");
}finally {
System.out.println(1);
a="a2";   //有异常会重新赋值给a 变量
System.out.println(2);
}
System.out.println(3); //会输出 捕捉到了异常没有从上面第一个return a返回 而是从下面这个return返回
return a;
}

public static void main(String[] args) {
CatchExecuteJustOne cejo = new CatchExecuteJustOne();
System.out.println(cejo.methodOne());
}
}

try中return 有异常的情况输出:

into methodOne method
catch 1
1
2
3
a2

 

Atas ialah kandungan terperinci Java基础值:异常处理关键字.... Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Isu terkini
Bolehkah java digunakan sebagai bahagian belakang web?
daripada 1970-01-01 08:00:00
0
0
0
Pasang JAVA
daripada 1970-01-01 08:00:00
0
0
0
Tidak dapat memasang java
daripada 1970-01-01 08:00:00
0
0
0
Bagaimanakah php melaksanakan penyulitan sha1 java?
daripada 1970-01-01 08:00:00
0
0
0
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan