关于final字段在类初始化过程中抛出(throw)异常问题的处理解析:
原Class类结构:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.mysql.cj.jdbc.JdbcPropertySetImpl;
public class JDBCUtils {
private static final String name;
static {
InputStream in=null;
try {
Properties properties=null;
in=JdbcPropertySetImpl.class.getClassLoader().getResourceAsStream("test.properties");
// in=JdbcPropertySetImpl.class.getClassLoader().getResourceAsStream("test/test.properties");//正确路径
properties=new Properties();
properties.load(in);
name=properties.getProperty("");//name="";
} catch (IOException e) {
throw new RuntimeException("",e);//如果去掉throw,相当只打印异常,name编译无法通过
} finally {
if (in!=null) {
try {
in.close();
} catch (IOException e) {
}
}
}
System.out.println("1");
}
}
解