Java method Exception throw with return instance.

知识共享许可协议
Work by Jianfei is licensed under a Creative Commons wei 3.0 Unported License.

 

1. We will get an error if write the getInstance method like below, cause the StaticEnvLib() throw a exception but there is no try- catch to catch it during the return new method.

 
  
   
public static StaticEnvLib getInstance()
     {
         
return new StaticEnvLib(); 
     }

public StaticEnvLib() throws IOException {     
     
        String filename 
= "H:\\Darwin.ini";
        BufferedReader reader 
= new BufferedReader(new InputStreamReader(     
                
new FileInputStream(filename), "UTF-8"));     
        read(reader);     
        reader.close();     
    }   

2. Modify the StaticEnvLib to like below.

public StaticEnvLib()  {     
     
        String filename 
= "H:\\Darwin.ini";
        BufferedReader reader 
= new BufferedReader(new InputStreamReader(     
                
new FileInputStream(filename), "UTF-8"));     
        read(reader);     
        reader.close();     
    } 

知识共享许可协议
Work by Jianfei is licensed under a Creative Commons wei 3.0 Unported License.
posted on 2011-04-07 10:00  zencorn  阅读(176)  评论(0编辑  收藏  举报