1.若finally前面有return,怎么样?
答: finally会在return前执行
例题一:
public classTest {
public static void main(String[] args) {
System.out.println(newTest().test());
}
staticint test()
{
int x = 1;
try
{
return x;
}
finally
{
++x;
}
}
}
返回1,说明是x先放到返回的填充的地方,然后再在finally加一,但对返回无影响,然后return 返回;
例题二:返回:2
public classTest {
public static void main(String[] args) {
System.out.println(newTest().test());
}
staticint test()
{
int x = 1;
try
{
return ++x;
}
finally
{
++x;
}
}
例题三:返回:1
public class MyTest {
static byte[] buffer = new byte[1024];
public static void main(String[] args) {
int n = -1;
try {
n = getString();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("result=" + n);
}
public static int getString() throws MyException {
FileInputStream fi = null;
int n = 0;
try {
boolean b = false;
fi = new FileInputStream(new File("./2.txt"));
n = fi.read(buffer);
n = 3;
return n;
} catch (FileNotFoundException e) {
e.printStackTrace();
return ++n;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
n++;
if (fi != null) {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return n;
}
}
例题四:报错,且返回:-1,
java.lang.NullPointerException
at com.jimmy.MyTest.getString(MyTest.java:58)
at com.jimmy.MyTest.main(MyTest.java:20)
result=-1
public class MyTest {
static byte[] buffer = new byte[1024];
public static void main(String[] args) {
int n = -1;
try {
n = getString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("result=" + n);
}
public static int getString() throws MyException {
FileInputStream fi = null;
int n = 0;
try {
boolean b = false;
//fi = new FileInputStream(new File("./2.txt"));注意这里注销了,fi为空
n = fi.read(buffer);
n = 3;
return n;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return ++n;
} catch (IOException e) {
e.printStackTrace();
} finally {
n++;
try {
fi.close(); // 同样fi为空,但是这里没有NullPointerException的处理,所以往上抛到了,被getString()的异常处理捕获
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return n;
//这里没执行
}
}
例题五(reference:http://blog.youkuaiyun.com/hguisu/article/details/6155636):返回:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false
public class MyExceptionTest {
public MyExceptionTest() {
}
boolean testEx() throws Exception {
boolean ret = true;
try {
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception"); //没有异常被抛出,这里不执行
ret = false;
throw e;
} finally {
System.out.println("testEx, finally; return value=" + ret);
return ret;
}
}
boolean testEx1() throws Exception {
boolean ret = true;
try {
ret = testEx2();
if (!ret) {
return false;
}
System.out.println("testEx1, at the end of try");
return ret;
} catch (Exception e) {
System.out.println("testEx1, catch exception");
//没有异常被抛出,这里不执行
ret = false;
throw e;
} finally {
System.out.println("testEx1, finally; return value=" + ret);
return ret;
}
}
boolean testEx2() throws Exception {
boolean ret = true;
try {
int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
return true;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e; //这里相当于return,可是return前要运行finally ,可在finally里有return ,就提前返回了
} finally {
System.out.println("testEx2, finally; return value=" + ret);
return ret;
}
}
public static void main(String[] args) {
MyExceptionTest testException1 = new MyExceptionTest();
try {
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
}
}
}