- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
异常处理 List.1 ExceptDemoExceptDemo.java.doc
异常处理 List.1 ExceptDemo/ExceptDemo.java
Return to top
001: class NewMathException extends Exception {
002: // Constructor
003: public NewMathException(double b, double e) {
004: super(Domain error: base = + b + exp = + e);
005: }
006: }
007:
008: final class NewMath {
009: // Prevent instantiation of class
010: private NewMath() { }
011: // Return b raised to the power of e
012: public static double power(double b, double e)
013: throws NewMathException {
014: NewMathException error = new NewMathException(b, e);
015: if (b 0.0) return Math.pow(b, e);
016: if (b 0.0) {
017: Double d = new Double(e);
018: double ipart = d.intValue();
019: double fpart = e - ipart;
020: if (fpart == 0) {
021: if ((ipart % 2) != 0) // i.e. ipart is odd
022: return -Math.pow(-b, e);
023: else
024: return Math.pow(-b, e);
025: } else
026: throw error;
027: } else {
028: if (e == 0.0) return 1.0;
029: if (e 1.0) throw error;
030: return 0.0;
031: }
032: }
033: }
034:
035: class ExceptDemo {
036: public static void main(String args[]) {
037: if (args.length 2) {
038: System.out.println(Specify value and exponent);
039: System.out.println(ex. java ExceptDemo -4 1.5);
040: }
041: else
042: try {
043: double base = new Double(args[0]).doubleValue();
044: double exponent = new Double(args[1]).doubleValue();
045: double result = NewMath.power(base, exponent);
046: System.out.println(Result = + result);
047: } catch (NewMathException e) {
048: System.out.println(e.getMessage());
049: }
050: }
051: }
Return to top
异常处理 List.2 FinallyDemo/FinallyDemo.java
Return to top
001: // Exception class
002: class ThrowMe extends Exception {
003: ThrowMe() { }
004: ThrowMe(String s) {
005: super(s);
006: }
007: }
008:
009: class FinallyDemo {
010: // Test method -- pass 0, 1, or 2 for different exceptions
011: static void testMethod(int n) throws Exception, ThrowMe {
012: switch (
文档评论(0)