b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

java语言程序设计(14)

电脑杂谈  发布时间:2016-12-30 16:06:08  来源:网络整理

String sNumber = "";

while(true){

try {

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

sNumber = bf.readLine();

number = Double.parseDouble(sNumber);

break; //如果代码能执行到这一行,就说明没有抛出异常

} catch (IOException ioe) {

System.err.println("some IOException");

} catch (NumberFormatException nfe) {

System.err.println(sNumber " is Not a legal number!");

}

}

System.out.println(number);

直到用户输入正确的信息才会被该代码放过。这是一种简单的恢复模型的实现,挺耐看的,我很喜欢!

20、try、catch、finally内变量的作用域和可见性。

在try块内定义的变量,它在catch或者finally块内都是无法访问到的,并且在整个异常处理语句之外也是不可见的。补充一点初始化:第一个例中最后一句被注释掉了。number是在运行时由用户输入而初始化的,但是在编译时刻并没有初始化,编译器会抱怨的。

21、输出异常信息。捕捉到异常之后,通常我们会输出相关的信息,以便更加明确异常。

catch (Exception mex) {

System.err.println("caught a exception!");

}

用标准错误流System.err比System.out要好。因为System.out也许会被重定向,System.err则不会。

22、更高级的话题我会补充上的,但是我的肚子抛出了Hungry异常,我必须catch然后调用eat()方法补充能量。昨晚的鱿鱼盖浇饭很好吃…

读取配置文件:

package myapp.src;

import java.io.IOException;
import java.util.Properties;

/**
 * @author xing.gexing E-mail:xing.gexing@aliyun-inc.com
 * @version 创建时间:Oct 30, 2012 9:46:53 PM 类说明
 */
public class ReadConf {
	private String filename;
	private Properties properties;

	/**
	 * @param args
	 */
	public ReadConf() {
		this.filename = "config.property";
		this.properties = new Properties();
		try {
			properties.load(ReadConf.class.getResourceAsStream(filename));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @return the filename
	 */
	public String getFilename() {
		return filename;
	}

	/**
	 * @param filename
	 *            the filename to set
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}

	/**
	 * @return the properties
	 */
	public Properties getProperties() {
		return properties;
	}

	/**
	 * @param properties
	 *            the properties to set
	 */
	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ReadConf readConf = new ReadConf();
		System.out.println(readConf.getProperties().getProperty("endpoint"));

	}

}
配置文件:
endpoint=http://10.230.205.88:61085


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-23953-14.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...