1

Here I have used throws in method signature when i am going to call this method from some where else that is not asking me to handle the 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);
        }
    }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115

2 Answers2

3

when i am going to call this method from some where else that is not asking me to handle the exception.

Yes, because both are RuntimeExceptions and must not be caught.

Read the java tutorial about exceptions and unchecked exceptions.

Sometimes you see methods that declare RuntimeExceptions, like your method does. It is a way to document the exceptions that might be thrown even if you don't catch them.

In addition to user3168013's comment

how can we able to convert unchecked exception to checked exception .

Every exception can have a cause. A cause is another exception that lead to it. If it has no cause it is a root exception. So you can just create an instance of a checked exception, pass it the unchecked exception as it's cause and throw the checked expection.

For example define your checked exception

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

and then throw your own

void set(String data[]) throws DataFormatException {
    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 DataFormatException(e);
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new DataFormatException(e);
    }
}

Of course it would be better to give a detailed exception message depending on the cause, but this is just a short example.

René Link
  • 48,224
  • 13
  • 108
  • 140
  • How the class user will get know about run time exception and how to handle it. – user3168013 Jan 08 '14 at 09:03
  • You can handle them the same way as checked exceptions. Use a try/catch. The class user will only know that your method throws a RuntimeException if you tell it (javadoc or declare them in the method signature). I would recommend you to read http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html – René Link Jan 08 '14 at 09:07
  • There can I use throw to handle exception. – user3168013 Jan 08 '14 at 09:14
  • how can we able to convert unchecked exception to checked exception . – user3168013 Jan 08 '14 at 09:35
  • @user3168013 I have updated my answer. Hope that helps. – René Link Jan 08 '14 at 09:42
  • super(cause); here am getting exception. – user3168013 Jan 08 '14 at 10:00
  • @user3168013 what exception do you get? – René Link Jan 08 '14 at 10:05
  • constructor Exception in class Exception cannot be applied to given types; required: no arguments found: Throwable reason: actual and formal argument lists differ in length – user3168013 Jan 08 '14 at 10:08
  • @user3168013 ok, I guess you put the `DataFormatException` or your own exception in the same package than your example class. Because your example class is named `Exception` it will be used instead of `java.lang.Exception`. Rename your example code `Exception` to `ExceptionExample` or something else or use the full quallified name when you extend from `java.lang.Exception`. – René Link Jan 08 '14 at 10:08
  • @user3168013 the simplest way is to use `public class DataFormatException extends java.lang.Exception {` – René Link Jan 08 '14 at 10:10
  • Ok i got that one can u plz tell me how to use that with out using try catch over there – user3168013 Jan 08 '14 at 10:28
  • There is simply no other way to do it. If a method throws an unchecked exception and you want an checked exception you must catch the unchecked, create a checked exception and throw it. – René Link Jan 08 '14 at 11:04
  • public class DataFormatException extends Exception { public DataFormatException(Throwable cause) { super(cause); } } how will cme to know this will create checked exception. – user3168013 Jan 08 '14 at 11:12
  • You can find the answer in the tutorial link: "Errors and runtime exceptions are collectively known as unchecked exceptions.". So when you extend `Exception` it is a checked exception. – René Link Jan 08 '14 at 11:51
1

Both Exceptions are unchecked Exception. Unchecked Exception are handled at runtime.

Its developer choice how to handle or not runtime Exception , compiler never force you handle.

Find more on handling Runtime Exception.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103