有一段代码
它是一个方法中间的一段代码
try{
if(file.length() <= 0 ) {
*******
}
(这后面还有很多代码) }catch(Exception e){
System.out.println("files1");
}那么在运行的时候,如果file 是null的话.那么后面的代码是不是不再去执行了呢.如果我在这个方法的前面加了一个
public void test() throws Exception {
}
效果和这个是一样的吗?也是后面的代码不执行吗...那么如果是这样的呢
public void test() throws Exception {
try {
} catch (Exception e) {
// 自己的一些处理
throw e;
}
} 希望高手详细说说
它是一个方法中间的一段代码
try{
if(file.length() <= 0 ) {
*******
}
(这后面还有很多代码) }catch(Exception e){
System.out.println("files1");
}那么在运行的时候,如果file 是null的话.那么后面的代码是不是不再去执行了呢.如果我在这个方法的前面加了一个
public void test() throws Exception {
}
效果和这个是一样的吗?也是后面的代码不执行吗...那么如果是这样的呢
public void test() throws Exception {
try {
} catch (Exception e) {
// 自己的一些处理
throw e;
}
} 希望高手详细说说
如果是Null,后边确实是不执行的,但是没有异常抛出,因为不执行了,是没有异常的。
不执行跟异常是两回事。
catch是捕获异常
当然不一样
作用正好相反
try{
if(file.length() <= 0 ) {
*******
}
(这后面还有很多代码) }catch(Exception e){
System.out.println("files1");
}如果出现异常,中文部分的代码是不执行的,即使throws Exception也不会执行。public void test() throws Exception {
try {
} catch (Exception e) {
// 自己的一些处理
throw e;
}
}
catch里面在throw语句前的代码会执行,一般情况下catch最后还要加一个finally语句模块,即使有异常,finally语句里面的代码都会执行
如果在方法声明的地方抛出异常,后边的代码也是不会执行,如果要进行一些处理,最好放在finally块里边
public static void main(String args[]){
Test t=new Test();
t.test();
System.out.println("a="+a);
}
public void test(){
try{
int c=b/a;
a=1;
}catch(Exception e){
System.out.println("error");
}
}
}输出:
error
a=0改成你的第二种形式public class Test { static int a=0,b=7;
public static void main(String args[]){
Test t=new Test();
try{
t.test();
System.out.println("a="+a);
}catch(Exception e){
System.out.println("error2");
}
}
public void test() throws Exception{
try{
int c=b/a;
a=1;
}catch(Exception e){
System.out.println("error");
}
}
}输出:
error
a=0
这种形式与第一种的区别在于你在test()方法那抛出了异常,这样的话你必须在调用方法的地方捕获异常,否则会编译不通过报异常,但需要注意的是,由于你在
catch(Exception e){
System.out.println("error");
}
中没有声明throw e将异常抛出,所以在执行到t.test();时并不会真正捕获异常,此时也就不会执行
catch(Exception e){
System.out.println("error2");
}
而是继续向下执行System.out.println("a="+a);第三种形式public class Test { static int a=0,b=7;
public static void main(String args[]){
Test t=new Test();
try{
t.test();
System.out.println("a="+a);
}catch(Exception e){
System.out.println("error2");
}
}
public void test() throws Exception{
try{
int c=b/a;
a=1;
}catch(Exception e){
System.out.println("error");
throw e;
}
}
}输出:
error
error2
我想你应该能明白了
catch里面在throw语句前的代码会执行,
这个就是throws和throws的区别
多看下关于异常的书,书上都对这两者做了详细的比较!!!
如果在你的程序中,哪个catch捕获了属于它的异常的话,后面的catch也不会执行了,但是final后面的语句是一定会执行的!
异常处理机制是在代码出现异常的时候,new了一个异常实例。
如果在catch块里都没有匹配的异常类型,则会交由上一级处理。一个声明了:throws Exception 的方法里,如果有try{},
则在try{}里出现的异常,又找到了catch块里匹配的异常类型,也不会向上抛异常。如果既没有try块,也没有 throws Exception,则代码里出现异常,会交由main处理。
如:Exception in thread "main" java.lang.NullPointerException
at com.lim.test.CaiPiao.test(CaiPiao.java:28)
at com.lim.test.CaiPiao.main(CaiPiao.java:19)
try{
if(file.length() <= 0 ) {
*******
}
(这后面还有很多代码) }catch(Exception e){
System.out.println("files1");
}二:public void test() throws Exception {
if(file.length() <= 0 ) {
*******
}
*******//其它代码
}
三:public void test() throws Exception {
try {
if(file.length() <= 0 ) {
*******
}
*******//其它代码 }
catch (Exception e) {
// 自己的一些处理
throw e;
}
} 只会执行红色的部分~其它的楼上的诸位都讲得蛮清楚了,我就只简言之.