正在加载图片...
Java实例 抛出异常 public class Exception3{ public static int Sum(int n){ if (n <0) throw new IllegalArgumentException("n应该为正整数!"); int s=0; for (int i=0;i<=n;it+)s =s i; return s; public static void main(String args[]){ try( int n Integer.parseInt(args[0]); System.out.println(Sum(n)); catch (ArrayIndexOutofBoundsException e) {System.out,printIn("命令行为:"+"java Exception:3<number>"W;J catch (NumberFormatException e2) {System.out,printin("参数<number>应为整数!");) catch (IllegalArgumentException e3) (System,out.printin("错误参数:"+e3.tostring(0);) finally System,out.printin("程序结束!");)Java实例——抛出异常 public class Exception3{ public static int Sum(int n){ if (n < 0) throw new IllegalArgumentException("n应该为正整数!"); int s = 0; for (int i=0; i<=n; i++) s = s + i; return s; } public static void main(String args[]){ try{ int n = Integer.parseInt(args[0]); System.out.println(Sum(n)); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("命令行为:"+"java Exception3 <number>"); } catch (NumberFormatException e2) { System.out.println("参数<number>应为整数!"); } catch (IllegalArgumentException e3) { System.out.println("错误参数:"+e3.toString()); } finally { System.out.println("程序结束!"); } } }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有