top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java: What will happen if a method does not throw an checked Exception directly but calls a method that does?

+2 votes
566 views
Java: What will happen if a method does not throw an checked Exception directly but calls a method that does?
posted Sep 23, 2013 by Vivek Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

This will be an compilation error saying "Unhandled exception" . The exceptions either needs to be handled or thrown.

answer Sep 23, 2013 by Vikalp Kumar
+1 vote

If a method does not throw an checked Exception directly but calls a method that throws an exception then the calling method must handle the throw exception or declare the exception in its throws clause. If the calling method does not handle and declares the exception the exceptions is passed to the next method in the method stack. This is known as ducking the exception down the method stack.

e.g. The code below 'll not compile as the getCar() method has not declared the CarNotFoundException which is thrown by the getColor () method.

void getFlower() {

getType();

}

void getFlower () {

throw new CarNotFoundException();

}

Fix for the above code is

void getFlower() throws CarNotFoundException {

getType();

}

void getFlower () {

throw new CarNotFoundException();

}

answer Sep 24, 2013 by Arvind Singh
Similar Questions
+2 votes

In LTE, the transport block size is a mapping of the CQI, MCS and the allocated PRB and does not depend on the CFI. So for the same TBSize (which means same CQI, MCS, allocated PRB), if the CFI is changed (as in case of dynamic CFI) what will happen? is the code rate will be increased or decreased?

...