Scope description of variables in try-catch statement block
The scope of a variable in a Try-catch statement block is the same as the scope of other statement blocks. Variables defined inside a statement block, scoped inside a statement block, and not externally visible.
/* Statement block Internal
*
/try{int a = 0;
} catch (Exception e) {
int b = 0;
} finally{
int c = 0;
}
/* Statement Block external * *
//a = 5; Illegal, the compiler cannot recognize the variable
//b = 5; Illegal, the compiler cannot recognize the variable
//c = 5; Illegal, the compiler does not recognize the variable
A variable defined outside a statement block that can be modified within a statement block.
/* Statement Block external *
/int a = 0;
/* Statement block Internal
*
/try{a = 5;
} catch (Exception e) {
a = 5;
} finally{
a = 5;
}
/* Statement Block external * *
System.out.println (a);
Run result is
5