Could a final variable be reassigned in catch, even if assignment is last operation in try?

前端 未结 12 1128
粉色の甜心
粉色の甜心 2020-12-04 18:49

I am quite convinced that here

final int i;
try { i = calculateIndex(); }
catch (Exception e) { i = 1; }

i cannot possibly have

12条回答
  •  -上瘾入骨i
    2020-12-04 19:41

    Not quite as clean (and I suspect what you are already doing). But this only adds 1 extra line.

    final int i;
    int temp;
    try { temp = calculateIndex(); }
    catch (IOException e) { temp = 1; }
    i = temp;
    

提交回复
热议问题