Redian新闻
>
1 quick interview question
avatar
1 quick interview question# Java - 爪哇娇娃
Z*Z
1
for a try/catch/finally block when is the finally block not executed?
avatar
w*s
2
return before finally?

【在 Z*****Z 的大作中提到】
: for a try/catch/finally block when is the finally block not executed?
avatar
g*e
3
System.exit
runtime error
when a daemon thread is abandomed

【在 Z*****Z 的大作中提到】
: for a try/catch/finally block when is the finally block not executed?
avatar
g*e
4
no

【在 w*******s 的大作中提到】
: return before finally?
avatar
Z*Z
5
not working
public class Test {

void f(){
try{
System.out.println("in a try block");
return ;
} finally{
System.out.println("finally");
}
}


public static void main(String[] args){
new Test().f();
}
}

【在 w*******s 的大作中提到】
: return before finally?
avatar
Z*Z
6
awesome, let me verify a little.
thanx!

【在 g**e 的大作中提到】
: System.exit
: runtime error
: when a daemon thread is abandomed

avatar
Z*Z
7
System.exit daemon thread are correct
the second reason seems not correct
public class Test {

void f(){
try{
System.out.println("in a try block");
//throw new Error("error"); // not working
throw new RuntimeException(); // not working
} finally{
System.out.println("finally");
}
}


public static void main(Str

【在 g**e 的大作中提到】
: System.exit
: runtime error
: when a daemon thread is abandomed

avatar
g*e
8
i shouldn't say "run time error". i actually mean the thread or jvm got
killed or crashed in try/catch block.
finally block will be executed definitely in your code below.

【在 Z*****Z 的大作中提到】
: System.exit daemon thread are correct
: the second reason seems not correct
: public class Test {
:
: void f(){
: try{
: System.out.println("in a try block");
: //throw new Error("error"); // not working
: throw new RuntimeException(); // not working
: } finally{

avatar
Z*Z
9
Yep, i know the RuntimeException one will definitely be executed. I was hopi
ng throwing an Error will make a change, which apparently not ...

【在 g**e 的大作中提到】
: i shouldn't say "run time error". i actually mean the thread or jvm got
: killed or crashed in try/catch block.
: finally block will be executed definitely in your code below.

avatar
g*g
10
When a thread is interrupted.

hopi

【在 Z*****Z 的大作中提到】
: Yep, i know the RuntimeException one will definitely be executed. I was hopi
: ng throwing an Error will make a change, which apparently not ...

avatar
Z*Z
11
i dont think so
public class Test extends Thread{

@Override
public void run(){
try{
while(true){
System.out.println("in a try block");
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally{
System.out.println("finally");


【在 g*****g 的大作中提到】
: When a thread is interrupted.
:
: hopi

avatar
g*g
12
When you don't catch InterruptedException of course.

【在 Z*****Z 的大作中提到】
: i dont think so
: public class Test extends Thread{
:
: @Override
: public void run(){
: try{
: while(true){
: System.out.println("in a try block");
: Thread.sleep(500);
: }

avatar
Z*Z
13
you mean, like this? still not working ...
public class Test extends Thread{

@Override
public void run(){
try {
f();
} catch (InterruptedException e) {
e.printStackTrace();
}
}


void f() throws InterruptedException{
try{
while(true){
System.out.println("in a try block");


【在 g*****g 的大作中提到】
: When you don't catch InterruptedException of course.
avatar
s*8
14
when machine crashes, or the process is killed.
Q: for a try/catch/finally block when is the finally block not executed?
A: when machine crashes, or the process is killed. A finally is a finally,
very simple, period. What do you think?
avatar
Z*Z
15
i think u r cool. but i dont want to be too cool to my interviewer.

,

【在 s***8 的大作中提到】
: when machine crashes, or the process is killed.
: Q: for a try/catch/finally block when is the finally block not executed?
: A: when machine crashes, or the process is killed. A finally is a finally,
: very simple, period. What do you think?

avatar
m*t
16
That's right! It also happened last time when I tripped the wire, and also
the weekend before that, when the earth was hit by that damn meteor!

,

【在 s***8 的大作中提到】
: when machine crashes, or the process is killed.
: Q: for a try/catch/finally block when is the finally block not executed?
: A: when machine crashes, or the process is killed. A finally is a finally,
: very simple, period. What do you think?

avatar
g*e
17
haha

【在 m******t 的大作中提到】
: That's right! It also happened last time when I tripped the wire, and also
: the weekend before that, when the earth was hit by that damn meteor!
:
: ,

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。