Exception Exception Class Not Found Exception Class Not

  • Slides: 11
Download presentation

Exception子类的继承关系 Exception Class. Not. Found. Exception Class. Not. Supported. Exception Illegal. Access. Exception Instantiation.

Exception子类的继承关系 Exception Class. Not. Found. Exception Class. Not. Supported. Exception Illegal. Access. Exception Instantiation. Exception Interrupted. Exception No. Such. Method. Exception Runtime. Exception Arithmetic. Exception Array. Store. Exception Class. Cast. Exception Illegal. Argument. Exception Illegal. Thread. State. Exception Number. Format. Exception 参见图 9. 2 Exception子类的继承关系 ……

【 9 -1】捕获数组下标越界异常 public class Exception 1 { public static void main(String args[]) {

【 9 -1】捕获数组下标越界异常 public class Exception 1 { public static void main(String args[]) { try { int a[]={1, 2, 3, 4, 5}, sum=0; for (int i=0; i<=5; i++) sum=sum+a[i]; System. out. println("sum="+sum); System. out. println("Successfully! "); } catch (Array. Index. Out. Of. Bounds. Exception e) { System. out. println("Array. Index. Out. Of. Bounds. Exception detected"); } finally { System. out. println(" Programm Finished! "); } }}

public static void main(String args[]){ try { int n = Integer. parse. Int(args[0]); System.

public static void main(String args[]){ try { int n = Integer. parse. Int(args[0]); System. out. println("sum="+Sum(n)); } catch (Array. Index. Out. Of. Bounds. Exception e 1) { System. out. println(e 1. to. String()); } catch (Number. Format. Exception e 2) { System. out. println("参数<number>应为整数!"); } finally { System. out. println("程序结束!"); } } }

为了明确一个方法不捕获异常,也可在声明该方法时,使用 throws选项,抛出该类异常。格式为: [修饰符] 返回值类型 方法名[(参数表)] throws 异常类名{ …… } v public class Exception 3

为了明确一个方法不捕获异常,也可在声明该方法时,使用 throws选项,抛出该类异常。格式为: [修饰符] 返回值类型 方法名[(参数表)] throws 异常类名{ …… } v public class Exception 3 { public static int Sum(int n) throws Array. Index. Out. Of. Bounds. Exception{ int s = 0; int x[ ]=new int[n]; for (int i=0; i<=n; i++) { x[i]=i; s = s +x[i]; } return s; }