The compiler stops you from doing something, which is most likely an error, since after your try-catch block you probably assume that the variable is initialized. However, if an exception is thrown, it will not be initialized.
- . , , , , .
, , , , :
String unknown = null;
try{
unknown="cannot see me, why?";
}catch(Exception e){
e.printStackTrace();
}
System.out.println(unknown);
- , , :
String unknown;
try{
unknown="cannot see me, why?";
}catch(Exception e){
e.printStackTrace();
unknown = "exception caught";
}
System.out.println(unknown);
, , , catch, , . :
String unknown;
try{
unknown="cannot see me, why?";
}catch(Exception e){
e.printStackTrace();
throw new Exception("Uh-oh...", e);
}
System.out.println(unknown);