| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 958 人关注过本帖
标题:异常处理
只看楼主 加入收藏
lbr2008
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2008-11-29
收藏
 问题点数:0 回复次数:4 
异常处理
高手!能不能帮我解决一下这个问题。
我这个代码用来测试“父类抛出多个异常。子类中的覆盖方法可以要抛出相应的异常或者抛出异常的子类,不能抛出新的异常”

class Ecp1 throws ArithmeticException
{
    private int a,b;
    public     int f1(a,b)
    {
    return a/b;
    }
}
class Ep2  extends Ecp1 throws  ArithmeticException,ArrayStoreException
{
    private int a,b;
    prinvate int[] ar=new int[5];
    public f2(a,b)
        {
            int c;
            int c=a/b;
            public int if(c>0)
            {
                System.out.println(c);
                return c;
            }
            else
            {
                for(int i=0;i<10;i++)
                {
                    ar[i]=c;
                }
            }
        }
}
public class ExceptionTest  
{
    public static void main(String[] args)
    {
        ExceptionTest et=new ExceptionTest();
        Ecp1 e1=new Ecp1();
        Ep2  e2=new Ep2();
        e1.f1(2,0);
        e2.f2(3,0);
    }
    
}
搜索更多相关主题的帖子: 异常处理 
2008-12-09 00:39
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
以下是引用lbr2008在2008-12-9 00:39的发言:

高手!能不能帮我解决一下这个问题。
我这个代码用来测试“父类抛出多个异常。子类中的覆盖方法可以要抛出相应的异常或者抛出异常的子类,不能抛出新的异常”

class Ecp1 throws ArithmeticException
{
    priva ...

程序代码:
class Ecp1{
    public void method1()throws ArithmeticException{
    }
}
class Ep2 extends Ecp1{
    public void method1()throws ArithmeticException,{
        throw new ArithmeticException();
    }
}
public class ExceptionTest{
    public static void main(String[] args){
        Ecp1 e1= new Ep2();
        e1.method1();
    }
}


好好看看java的语法吧……
2008-12-09 10:05
ngk123ngk
Rank: 1
等 级:新手上路
威 望:1
帖 子:11
专家分:0
注 册:2008-1-2
收藏
得分:0 
同样的问题。
class Ecp1 throws ArithmeticException
类不能抛出异常!
public     int f1(a,b)
a.b参数缺少类型。
代码修改后:
class Ecp1 {
    public int f1(int a,int b) throws ArithmeticException
    {
    return a/b;
    }
}

class Ep2 extends Ecp1
{
    private int[] ar=new int[5];
    public int f2(int a,int b) throws  ArithmeticException,ArrayStoreException
        {
            int c = a/b;
            if(c>0)
            {
                System.out.println(c);
                return c;
            }
            else
            {
                for(int i=0;i<10;i++)
                {
                    ar[i]=c;
                }
            }
        }
}
public class ExceptionTest  
{
    public static void main(String[] args)
    {
        ExceptionTest  et=new ExceptionTest();
        Ecp1 e1=new Ecp1();
        Ep2  e2=new Ep2();
        e1.f1(2,0);
        e2.f2(3,0);
    }
   
}
2008-12-09 10:26
ngk123ngk
Rank: 1
等 级:新手上路
威 望:1
帖 子:11
专家分:0
注 册:2008-1-2
收藏
得分:0 
最终代码修改,抱歉
class Ecp1 {
    public int f1(int a,int b) throws ArithmeticException
    {
    return a/b;
    }
}

class Ep2 extends Ecp1
{
    private int[] ar=new int[5];
    public void f2(int a,int b) throws  ArithmeticException,ArrayStoreException
        {
            int c = a/b;
            if(c>0)
            {
                System.out.println(c);
            }
            else
            {
                for(int i=0;i<10;i++)
                {
                    ar[i]=c;
                }
            }
        }
}
public class SaveFile  
{
    public static void main(String[] args)
    {
        Ecp1 e1=new Ecp1();
        Ep2  e2=new Ep2();
        try{e1.f1(2,0);}
        catch(ArithmeticException e){
            System.out.println("父类的异常:ArithmeticException");
        }
        try{e2.f2(3,0);}
        catch(ArithmeticException e){
            System.out.println("继承类的异常:ArithmeticException");
        }
        catch(ArrayStoreException e){
            System.out.println("继承类的异常:ArrayStoreExcepti");
        }

    }
   
}
2008-12-09 10:40
ngk123ngk
Rank: 1
等 级:新手上路
威 望:1
帖 子:11
专家分:0
注 册:2008-1-2
收藏
得分:0 
测试结果
继承类可以抛出新的异常,但是,我测试的只是系统异常
2008-12-09 11:04
快速回复:异常处理
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020053 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved