江西广告网
标题:
JAVA程序员必读:基础篇(2.d)语言基础
[打印本页]
作者:
织女
时间:
2008-12-27 17:42
标题:
JAVA程序员必读:基础篇(2.d)语言基础
语言基础 3.4.5 异常处理语句 JAVA编程语言提供一个异常机制用来帮助程序报告和处理错误。当一个错误发生的时候,程序就产生一个异常。即正常的程序路程被中断并且系统缓建想找到一个异常处理:一个能处理特殊类型错误的代码块。这个异常福利可以视图恢复错误或者如果它决定了错误是不可修复的,它提供一个较好退出程序的方法。 有三条语句可以处理异常: try语句,它可以在异常产生的地方识别语句块。 catch语句,它必须和try语句一起用,它可是识别可以处理特定异常错误的语句块。这个语句是在如果有一个特定的异常发生在try块中就被执行。 (3)finally语句,它必须跟try语句一起用,它可以识别语句块,这些语句块是在不管在try块中是否有错误发生被执行的。 下面是这三个语句的语法格式: try { 语句(参数) } catch (异常错误类型 名字) { 语句(参数) } finally { 语句(参数) } 上面简述了由JAVA编程语言提供的用来报告和处理错误的语句。但是,另外的因素和原因,比如运行和检查异常之间的差异以及异常类的分级结构(代表异常的不同种类)在使用异常机制的时候起着重要的作用。 3.4.6 分支语句 JAVA编程语言支持下面的三种分支结构: break语句 continue语句 return语句 下面逐个介绍: (1)break语句 break语句有两种形式:未标志的和标志的。你在前面就已经看过了未标志的break语句。一个未标志的break语句终止swtich语句,控制流程马上转到switch语句下方的语句。下面的例程BreakDemo,它包含了一个for循环查找数组中特定的数值: public class BreakDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; int searchfor = 12; int i = 0; boolean foundIt = false; for ( ; i < arrayOfInts.length; i ) { if (arrayOfInts[i] == searchfor) { foundIt = true; break; } } if (foundIt) { System.out.println("Found " searchfor " at index " i); } else { System.out.println(searchfor "not in the array"); } } } 当数值被找到的时候,这个break语句终止了for循环。控制流程就转到for语句的下面的语句继续执行。这个程序的输出为: Found 12 at index 4 3.4.6 分支语句 未标志形式的break语句被用来终止内部的switch、for、while或者do-while。而标志形式的break语句终止一个外部的语句,它是通过在break语句中使用一个标志来实现的。下面的例程BreakWithLabelDemo跟前面的例子有点相似,只不过它是在一个两维数组中搜索一个数值。利用两个嵌套的for循环遍历了整个数组。当数值被找到了,标志形式的break语句就终止标志的search语句,这个search语句是在for循环外部的: public class BreakWithLabelDemo { public static void main(String[] args) { int[][] arrayOfInts = { { 32, 87, 3, 589 }, { 12, 1076, 2000, 8 }, { 622, 127, 77, 955 } }; int searchfor = 12; int i = 0; int j = 0; boolean foundIt = false; search: for ( ; i < arrayOfInts.length; i ) { for (j = 0; j < arrayOfInts[i].length; j ) { if (arrayOfInts[i][j] == searchfor) { foundIt = true; break search; } } } if (foundIt) { System.out.println("Found " searchfor " at " i ", " j); } else { System.out.println(searchfor "not in the array"); } } } 这个程序的输出为: Found 12 at 1, 0 这个语法看起来可能有点费解。break语句终止了标志语句,它不能将控制流程转到这个标志处。控制流程迅速将标志(终止的)后面的语句。 (2)continue语句 你可以使用continue语句来跳过当前的for、while或者do-while循环。未标志形式跳到内部循环体的末尾处并且计算控制循环的boolean表达式,跳过循环的其它部分。下面的例程ContinueDemo遍历字符串中的所有字母。如果当前字母不是一个p,contiue语句就忽略循环的其它部分并且处理下一个字符。如果它是一个p字母,程序就对计数器增1,再将p转换维大写字母: public class ContinueDemo { public static void main(String[] args) { StringBuffer searchMe = new StringBuffer( "peter piper picked a peck of pickled peppers"); int max = searchMe.length(); int numPs = 0; for (int i = 0; i < max; i ) //interested only in p's if (searchMe.charAt(i) != 'p') continue; //process p's numPs ; searchMe.setCharAt(i, 'P'); } System.out.println("Found " numPs " p's in the string."); System.out.println(searchMe); } } 这个程序的输出为: Found 9 p's in the string. Peter PiPer Picked a Peck of Pickled PePPers 标志形式的continue语句将忽略外部给定标志的循环。下面的例程ContinueWithLabelDemo,它使用了一个嵌套的循环来搜索一个子字符串。程序如下: public class ContinueWithLabelDemo { public static void main(String[] args) { String searchMe = "Look for a substring in me"; String substring = "sub"; boolean foundIt = false; int max = searchMe.length() - substring.length(); test: for (int i = 0; i <= max; i ) { int n = substring.length(); int j = i; int k = 0; while (n-- != 0) { if (searchMe.charAt(j ) != substring.charAt(k )) { continue test; } } foundIt = true; break test; } System.out.println(foundIt ? "Found it" : "Didn't find it"); } } 这个程序的输出为:Found it (3)return 语句 最后讲讲分支结构的最后一个return语句。你可以使用return 来退出当前的方法。控制流程返回到调用方法的下一个语句。这个return语句有两种形式:一种是返回一个数值,另外一种没有返回数值。为了返回一个数值,简单地,可以将数值放置在return关键字后面即可。例如: return count; 由return返回的数值类型必须匹配方法声明返回的数值类型。当方法被声明void,return的使用就不返回一个数值: return
欢迎光临 江西广告网 (http://bbs.jxadw.com/)
Powered by Discuz! X3.2