user3168013
user3168013

Reputation: 13

How to convert this run time exception into checked exception?

Hi how to convert this run time exception into checked exception this method has to force the class user to handle the exception specified in the method signature. This exceptions are unchecked exception.

 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);
            }
        }

Upvotes: 0

Views: 2401

Answers (6)

ufis
ufis

Reputation: 176

Wrap it in your own defined Exception

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

    //implement other constructors too
}

Then change your method to

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

Upvotes: 0

Narendra Pathai
Narendra Pathai

Reputation: 42005

Just wrap it into an checked exception applicable in your context

throw new Exception(runtimeException);

I have used Exception but you can use your own CustomException which extends Exception and wrap the RuntimeException into it.

TIP:

Also make sure you want to use checked exception for similar scenarios. Runtime exceptions are kept runtime for a reason that the situation cannot be recovered from. So make sure that the runtime exception that you are wrapping will be helpful as a checked exception. Caller should be able to derive some value from it.

Secondly as I told in the TIP that caller will in no way be able to recover from ArrayIndexOutOfBoundsException, it will not serve any purpose to the caller.

EDIT:

I am showing you the demo but I am no way in favor of doing so. Firstly the ArrayIndexOutOfBoundsException should never be thrown, you should take care that array does not go out of index in your code.

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

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

Upvotes: 2

Pradeep Kr Kaushal
Pradeep Kr Kaushal

Reputation: 1546

You can catch the RunTime exception if you want to convert it into Checked exception. In the following code snippet if set() is being used, then it will forced user to do checked exception.

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();
        }
    }

Upvotes: 0

zolt
zolt

Reputation: 103

What do you mean? You want extends your class from RuntimeException, or what? If you want catch RuntimeException in this block:

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;
}    

so use try/catch construction, like that

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);   
}  

Upvotes: 0

Rugal
Rugal

Reputation: 2717

You can catch exception in block rather than let it throw up:

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);
    }     
}

Upvotes: 0

Balduz
Balduz

Reputation: 3570

Make your Exception class extend a checked exception.

Upvotes: 0

Related Questions