有沒(méi)有高手幫我看看代碼怎么運(yùn)行不起!
package?immoc.com.trycatch; public?class?Test?{ /* ?*?拋出?數(shù)組越界和算術(shù)異常 ?*/ ?public?void?Test1(int?x)?throws?ArrayIndexOutOfBoundsException,ArithmeticException{ ?????System.out.println(x); ?????if(x?==?0){ ?????????System.out.println("沒(méi)有異常"); ?????????return; ?????} ?????//數(shù)據(jù)越界異常 ?????else?if?(x?==?1){ ?????????int[]?a?=?new?int[3]; ??????????a[3]?=?5; ?????} ?????//算術(shù)異常 ?????else?if?(x?==?2){ ?????????int?i?=?0; ?????????int?j?=?5/0; ?????} ?} }
package immoc.com.trycatch;
public class ExceptionInital {
?/**
? * @param args
? */
public static void main(String[] args){
?//創(chuàng)建對(duì)象
??? ExceptionInital object = new ExceptionInital();
??? // 調(diào)用會(huì)拋出異常的方法,用try-catch塊
??? try{
??????? object.Test1(0);
??? }catch(Exception e){
??????? System.out.println(e);
??? }
??? // 數(shù)組越界異常
??? try{
??????? object.Test1(1);
??? }catch (ArrayIndexOutOfBoundsException e) {
??????? System.out.println("數(shù)組越界異常:"+e);
??? }
??? // 算術(shù)異常
??? try{
??????? object.Test1(2);
??? }catch(ArithmeticException e){
??????? System.out.println("算術(shù)異常:"+e);
??? }
??? //使用 throw 拋出異常(可以拋出異常對(duì)象,也可以拋出異常對(duì)象的引用)
??? try{
??????? ArrayIndexOutOfBoundsException? exception = new ArrayIndexOutOfBoundsException();
??????? throw exception;//new ArrayIndexOutOfBoundsException();
??? }catch(ArrayIndexOutOfBoundsException e){
??????? System.out.println("thorw拋出異常:"+e);
??? }
}
2017-12-18
public static void main(Strring[] args),這里多了個(gè)r,應(yīng)該是String[]
ExceptionInital類中沒(méi)有定義test1(),自己也是小白,還不知道怎么解決
希望能幫到你
2017-12-18
你自己放Eclipse里面看看
public static void main(Strring[] args)這里錯(cuò)了
而且你要調(diào)用的Test1方法在Test類里面。所以object的類型應(yīng)該是Test
其他的你在自己看吧。