一个你会忽略的问题。。。今天被问到了。。。。自己才发现什么也不知道。。。。
第一种:
public
class
TryCatchTest {
public
static
void
main(String[] args) {
System.
out
.println(fun());
}
public
static
String fun(){
try
{
int
temp =10/0;
return
"abc"
;
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
System.
out
.println(
"NO"
);
}
return
"aaa"
;
}
}
结果:
java.lang.ArithmeticException
: / by zero
at test.TryCatchTest.fun(
TryCatchTest.java:13
)
at test.TryCatchTest.main(
TryCatchTest.java:7
)
NO
aaa
第二种
public
class
TryCatchTest {
public
static
void
main(String[] args) {
System.
out
.println(fun());
}
public
static
String fun(){
try
{
int
temp =10/0;
return
"abc"
;
}
catch
(Exception e) {
e.printStackTrace();
return
"aaa"
;
}
finally
{
System.
out
.println(
"NO"
);
}
}
}
结果:
java.lang.ArithmeticException
: / by zero
at test.TryCatchTest.fun(
TryCatchTest.java:13
)
at test.TryCatchTest.main(
TryCatchTest.java:7
)
NO
aaa
第三种
public
class
TryCatchTest {
public
static
void
main(String[] args) {
System.
out
.println(fun());
}
public
static
String fun(){
try
{
int
temp =10/0;
return
"abc"
;
}
catch
(Exception e) {
e.printStackTrace();
return
"aaa"
;
}
finally
{
System.
out
.println(
"NO"
);
return
"bbb"
;
}
}
}
结果:
java.lang.ArithmeticException
: / by zero
at test.TryCatchTest.fun(
TryCatchTest.java:13
)
at test.TryCatchTest.main(
TryCatchTest.java:7
)
NO
bbb
第三种
public
class
TryCatchTest {
public
static
void
main(String[] args) {
System.
out
.println(fun());
}
public
static
String fun(){
try
{
int
temp =10/0;
}
catch
(Exception e) {
e.printStackTrace();
return
"aaa"
;
}
finally
{
System.
out
.println(
"NO"
);
}
return
"bbb"
;
}
}
java.lang.ArithmeticException
: / by zero
at test.TryCatchTest.fun(
TryCatchTest.java:13
)
at test.TryCatchTest.main(
TryCatchTest.java:7
)
NO
bbb
第四种:
public
class
TryCatchTest {
public
static
void
main(String[] args) {
System.
out
.println(fun());
}
public
static
String fun(){
try
{
int
temp =10/0;
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
System.
out
.println(
"NO"
);
return
"aaa"
;
}
return
"bbb"
;
//这里报错
}
}