如何将此运行时异常转换为已检查的异常?

时间:2014-01-08 09:41:55

标签: java exception-handling

如何将此运行时异常转换为已检查的异常,此方法必须强制类用户处理方法签名中指定的异常。此异常是未经检查的异常。

 public class Exception {

            int a, b, c;

            void set(String data[]) throws NumberFormatException, ArrayIndexOutOfBoundsException {
                a = Integer.parseInt(data[0]);//convert the string into int. eg1.("12" ---> 12)  eg2.("df23" ---> fail)
                b = Integer.parseInt(data[1]);
                c = 0;
            }

            void divide() throws ArithmeticException {
                c = a / b;
            }

            void disp() {
                System.out.println(a + " / " + b + " = " + c);
            }
        }

6 个答案:

答案 0 :(得分:2)

只需包装成适用于您的上下文的已检查例外

throw new Exception(runtimeException);

我使用了Exception,但您可以使用自己的CustomException extends Exception并将RuntimeException打包到其中。

提示:

还要确保要对类似方案使用已检查的异常。运行时异常保持运行时,原因是 无法恢复 。因此,请确保包装的运行时异常作为已检查的异常有用。来电者应该能够从中获取一些价值

其次,正如我在TIP中所说的那样,来电者无法从ArrayIndexOutOfBoundsException恢复,但它不会对来电者有任何作用。

修改

我正在向你展示演示,但我不赞成这样做。首先,永远不要抛出ArrayIndexOutOfBoundsException,你应该注意数组不会超出代码中的索引。

void set(String data[]) throws Exception{
     try{

     }catch(NumberFormatException ex){ 
         throw new Exception(ex);
     }catch(ArrayIndexOutOfBoundsException aiobe){
         throw new Exception(aiobe);
     }     
}

答案 1 :(得分:0)

Exception类扩展已检查的异常。

答案 2 :(得分:0)

你可以在块中捕获异常,而不是让它抛出:

public class Exception 
{
    int a, b, c;
    void set(String data[]) 
    {
        try 
        {
            a = Integer.parseInt(data[0]);
            b = Integer.parseInt(data[1]);
            c = 0;
        }
        catch(NumberFormatException nfe)
        {}
        catch(ArrayIndexOutOfBoundsException aiofbe)
        {}
        catch(Exception  e)
        {}
    }

    void divide()
    {
        try 
        {
            c = a / b;
        }
        catch(NumberFormatException nfe)
        {}
        catch(Exception  e)
        {}

    }

    void disp() 
    {        
        System.out.println(a + " / " + b + " = " + c);
    }     
}

答案 3 :(得分:0)

你是什​​么意思?你想从RuntimeException扩展你的类,或者什么? 如果你想在这个块中捕获RuntimeException:

void set(String data[]) throws NumberFormatException, ArrayIndexOutOfBoundsException {
a = Integer.parseInt(data[0]);//convert the string into int. eg1.("12" ---> 12)  eg2.("df23" ---> fail)
b = Integer.parseInt(data[1]);
c = 0;
}    

所以使用try / catch构造,就像那样

try{
   a = Integer.parseInt(data[0]);//convert the string into int. eg1.("12" ---> 12)  eg2.("df23" ---> fail)
   b = Integer.parseInt(data[1]);
   c = 0;
} catch (RuntimeException e) {
    throw MyCoolCheckedExceptin(e.getMessage, e);   
}  

答案 4 :(得分:0)

如果要将其转换为Checked异常,可以捕获RunTime异常。在以下代码片段中,如果正在使用set(),则会强制用户执行检查异常。

void set(String data[]) throws Exception  {
        try {
            a = Integer.parseInt(data[0]);//convert the string into int. eg1.("12" ---> 12)  eg2.("df23" ---> fail)
            b = Integer.parseInt(data[1]);
            c = 0;
        } catch (NumberFormatException e) {
         throw new Exception();
        }
    }

答案 5 :(得分:0)

将其包裹在您自己定义的Exception

class MyOwnArithmeticException extends Exception {
    public MyOwnArithmeticException(Throwable cause) {
        super(cause);
    }

    //implement other constructors too
}

然后将您的方法更改为

void divide() throws MyOwnArithmeticException {
    try {
        c = a / b;
    } catch (ArithmeticException e) {
        throw new MyOwnArithmeticException(e);
    }
}