I have 2 classes in my module where one of the class say Class A has methods which could throw InterruptedException, NoSuchElementException and another class say class B has methods which calls the method from class A. Could someone please guide me with what is a good practice to implement exception handling? Shall it be CASE 1 or CASE 2 or any other way to do so.
CASE 1 ::
Class A
methodA1 throws InterruptedException, NoSuchElementException {...} methodA2 throws InterruptedException, NoSuchElementException {...} . . . . methodA10 throws InterruptedException, NoSuchElementException {...} Class B
a = new A(); methodB1 { try{ a.methodA1(); a.methodA2(); } catch(InterruptedException){ //do something } catch(NoSuchElementException){ //do something else } } methodB2 { try{ a.methodA9(); a.methodA10(); } catch(InterruptedException){ //do something } catch(NoSuchElementException){ //do something else } } SCENARIO 2 ::
Class A
methodA1 { try{ //perform actions } catch(InterruptedException){ //do something } catch(NoSuchElementException){ //do something else } } . . . . methodA10 { try{ //perform actions } catch(InterruptedException){ //do something } catch(NoSuchElementException){ //do something else } } Class B
a = new A(); methodB1 { a.methodA1(); a.methodA2(); } methodB2 { a.methodA1(); a.methodA2(); }