当前位置: 首页 > news >正文

呼和浩特市网站建设_网站建设公司_虚拟主机_seo优化

黄岛网站建设,网站建设准备工作,中国大数据公司排名10强,外国人爱做视频网站有一点很重要,即你要时刻询问子句如果异常发生了,所有东西能被正确清理码?,尽管大多数情况下时非常安全的,但涉及到构造器时,问题出现了,构造器会把对象设置成安全的初始状态,但还会又别的动作,比如打开一个文件,这样的动作只有在对象使用完毕并且用户调用了特殊的…  有一点很重要,即你要时刻询问子句如果异常发生了,所有东西能被正确清理码?,尽管大多数情况下时非常安全的,但涉及到构造器时,问题出现了,构造器会把对象设置成安全的初始状态,但还会又别的动作,比如打开一个文件,这样的动作只有在对象使用完毕并且用户调用了特殊的清理方法之后才能得以清理,如果在构造器内抛出了异常,这些行为也许就不能正常工作了,这意味着在编写构造器时要格外细心.   用finally也许可以解决问起,但问题并非如此简单,因为finally会每次都执行清理操作,如果构造器在执行过程中半途而废,也许该对象的某部分还没有创建成功就要被清理,这又会抛出新的异常(.close()也会抛出异常)  1.构造器抛出异常要格外注意清理方法是否有必要调用,如果方法恰当,直接向上层抛出的确能简化编程 package exceptions; //: exceptions/InputFile.java // Paying attention to exceptions in constructors. import java.io.*;public class InputFile {private BufferedReader in;public InputFile(String fname) throws Exception {try {in new BufferedReader(new FileReader(fname));// Other code that might throw exceptions} catch(FileNotFoundException e) {System.out.println(Could not open fname);// Wasnt open, so dont close it //如果没有打开文件就不需要关闭throw e;} catch(Exception e) {// All other exceptions must close it 如果时其它异常则必须关闭文件try { //in.close()也可能抛出异常,所有要放到try块里面in.close();} catch(IOException e2) {System.out.println(in.close() unsuccessful);}throw e; // Rethrow} finally { //由于finally总会被执行,如果在这里关闭文件则文件刚打开还没开始使用就关闭了// Dont close it here!!!}}public String getLine() {String s;try {s in.readLine();} catch(IOException e) { //这这异常已被捕获,因此getLine不会抛出任何异常throw new RuntimeException(readLine() failed);//重新抛出新的异常到上层环境,有时会简化编程}return s;}public void dispose() {try {in.close();System.out.println(dispose() successful);} catch(IOException e2) {throw new RuntimeException(in.close() failed);}} } ///:~ 2.对于在构造器阶段可能抛出的异常,并且要求清理的,最安全的使用方法时使用嵌套的try子句, package exceptions; //: exceptions/Cleanup.java // Guaranteeing proper cleanup of a resource.public class Cleanup {public static void main(String[] args) {try {InputFile in new InputFile(Cleanup.java);try {String s;int i 1;while((s in.getLine()) ! null); // Perform line-by-line processing here...} catch(Exception e) {//这里捕捉的时getLine()方法重新抛出的异常System.out.println(Caught Exception in main);e.printStackTrace(System.out);} finally { //如果构造成功,则一定会执行in.dispose()清理in.dispose();}} catch(Exception e) { //InputFile对象在自己的try语句块优先,因此构造失败会进入这里,而不会执行内部的try块的in.colse()System.out.println(InputFile construction failed);}} } /* Output: dispose() successful *///:~   3. 这种通用的清理惯用法在构造器不跑出任何异常时也应该应用,其基本规则时:在需要清理的对象之后,立即进入一个try-finally语句块. //基本上,你应该仔细考虑所有的可能细节,例如本例的dispose()如果可以抛出异常,那么就需要额外的try语句块package exceptions; //: exceptions/CleanupIdiom.java // Each disposable object must be followed by a try-finallyclass NeedsCleanup { // Construction cant failprivate static long counter 1;private final long id counter;public void dispose() {System.out.println(NeedsCleanup id disposed);} }class ConstructionException extends Exception {}class NeedsCleanup2 extends NeedsCleanup {// Construction can fail:public NeedsCleanup2() throws ConstructionException {} }public class CleanupIdiom {public static void main(String[] args) {// Section 1:NeedsCleanup nc1 new NeedsCleanup();try {// ...} finally {nc1.dispose();}// Section 2:// If construction cannot fail you can group objects:// nc5 constructor 如果对象构造不能失败就不需要任何catch//不能失败的对象构造器对象可以群众在一起NeedsCleanup nc2 new NeedsCleanup();NeedsCleanup nc3 new NeedsCleanup();try {// ...} finally {nc3.dispose(); // Reverse order of construction nc2.dispose();}// Section 3:// If construction can fail you must guard each one:try {NeedsCleanup2 nc4 new NeedsCleanup2();try {NeedsCleanup2 nc5 new NeedsCleanup2();try { //如果nc5对象构造失败则会调用try块清理,否则永不调用// ...} finally {nc5.dispose();}} catch(ConstructionException e) { System.out.println(e);} finally {nc4.dispose();}} catch(ConstructionException e) { // nc4 constructorSystem.out.println(e);}} } /* Output: NeedsCleanup 1 disposed NeedsCleanup 3 disposed NeedsCleanup 2 disposed NeedsCleanup 5 disposed NeedsCleanup 4 disposed *///:~  转载于:https://www.cnblogs.com/jiangfeilong/p/10303179.html
http://www.ihoyoo.com/news/112706.html

相关文章:

  • 北京建网站公司推荐朝阳百姓网免费发布信息
  • 南充网站建设价格做网站排名多少钱
  • 影楼网站制作wordpress动漫主题曲
  • 教你做美食的网站wordpress怎么添加代码
  • 成品网站1688特色微九州合作网站
  • 网站雪花特效购物网站建设机构
  • 西安网站建设q.479185700強输入关键词自动生成文章
  • ps做图下载网站有哪些网站集约化建设 通知
  • 济南赢动网站建设做网站免费服务器哪家好
  • 网站维护都是一些什么公司酒店网站建设描述
  • 在网上建设网站网站已备案下一步怎么做
  • 移动端企业网站wordpress win10主题
  • 怎么样利用一些网站开发客户建设银行网站怎么开通手机短信
  • 杭州做网站的好公司有哪些莱芜口镇规划
  • 如何做汽车团购网站网站备案很麻烦吗
  • 网站界面排版eclipes 网站开发
  • 旅游网站建设可行性分析制作图片文字模板的软件
  • 个人建站如何赚钱长宁区网站建设网页制作
  • 网站兼容性xp做网站
  • 网站的行为怎么做医疗网页设计
  • 网站访问pv量是指什么怎么看网站有没有做推广
  • 怎样制作一个个人网站快速建站软件排名
  • 网站建设流程包括已经有域名,如何建设网站
  • 网站关键词优化seo关键词之间最好用逗号做影视网站怎么赚钱
  • 《网站建设与管理》论文河北省住建和城乡建设厅网站首页
  • 我们是谁 网站运营口腔网站建设
  • 晋城 网站建设长沙百度网站排名优化
  • 酒店网站开发协议网站可以做信息抓取吗
  • 网站建站公司公告竞价排名的定义
  • 博客社区类网站模板列举网站开发常用的工具