本帖最后由 陈圳 于 2013-2-26 14:57 编辑
- <div class="blockcode"><blockquote>package Day16;
- class suanFa
- {
- //一个返回最大值的方法
- public static int choose(int a,int b)
- {
- int m=0;
- try
- {
- if(a==0||b==0)
- throw new byZeroException("选择数不能为0");
- if(a>b)
- {
- System.out.println("return a");
- m=a;
- }
-
- else
- {
- System.out.println("return b");
- m=b;
- }
- }
- catch(byZeroException e)
- {
- System.out.println(e.toString());
- }
- finally
- {
- System.out.println("finally run");
- //通过finally改变返回值
- m=0;
- System.out.println("a="+a);
- }
- System.out.println("m="+m);
- return m; //
- }
- }
- //定义了异常处理函数
- class byZeroException extends RuntimeException
- {
- byZeroException(String msg)
- {
- super(msg);
- }
- }
- class Test2
- {
- public static void main(String[] args)
- {
- //输入反回值方法
- System.out.print(suanFa.choose(6,1));
- }
- <blockquote>/*
复制代码 |