1 Exceptions class El extends Exception ; class E2 extends El ( class E3 extends
ID: 3900124 • Letter: 1
Question
1 Exceptions class El extends Exception ; class E2 extends El ( class E3 extends El f^; class EA extends E3 class E5 extends E3 j; class F1 extends Exception ; class F2 extends F1 {}; class F3 extends F1 ; class F4 extends Fl ; In the following, we will use modifications to variables to summarise actions. This code that follows is intended to illustrate concepts and is deliberately not a model of clarity. 2 Exercise Assume x, y are member variables. Consider the following code: x=0, y=0; Work out the values of x, y if fC) were to throw the following types of exception: 1. F2 2. F3 3. F4 4. E4 try y+ 10 ) catch (F3 e) } catch (F1 e) { x+=10 catch (F4 e) x+=100; } catch (E5 e) [ y+-20 I catch (Exception e) [ y+-100;Explanation / Answer
Answering your all questions related to exception hanling in detail. Please ask the question regarding customer hashcode in a separate thread, as that is a separate coding exercise.
Within the given time limit, it is not possible to answer all the questions in detail. Hope you understand, Thank-you.
Exercise 2:
1. F2:
Because F2 is not of type F3, but is of type F1, hence 10 gets added to x. and then 20 is added to y.
So x = 10, y = 20;
2. F3:
First catch block is hit and 1 is added to x and then after try catch 20 is added to y.
So x = 1, y = 20;
3. F4:
Because F4 is not of type F3, but is of type F1, hence 10 gets added to x. and then 20 is added to y. this exception will not be able to reach till F4 cacth block.
So x = 10, y = 20;
4. E4:
E4 can not be caught by E5, Hence the Outer exception block is hit. So x remain 0, while y gets 100.
So x = 0, y = 100;
Exercise 3:
1. E1:
Exception is caught on first catch block itself. and then finally is also executed. SO y becomes 1(return value fo g) and x becomes 1 + 10 = 11
So x = 11, y = 1;
2. E2:
E1 block can catch E2 type of exception. Hence above behaviour:
So x = 11, y = 1;
3. E3:
E1 block can catch E3 type of exception. Hence above behaviour:
So x = 11, y = 1;
Exercise 4:
1. F3:
F3 will be caught in F1 catch block. Hence x becomes 1. then 10 is added to y. Outer catch blocks are not hit.
So x = 1, y= 10;
2. F2:
F2 is caught by first catch block. Hence x is incremented by 5. Then g() is called. Due to which catch block of next level is hit and 100 is added to y. Outer most exception block is not hit.
So x = 5, y = 100;
3. E1:
Exception is caught in E1 exception block, and 2 is added in y. Then again exception E1 is thrown, which will be caught in outermost catch block, so 1000 is added in x.
x = 1000, y = 2;
4. F1:
F1 will be caught in F1 catch block. Hence x becomes 1. then 10 is added to y. Outer catch blocks are not hit.
So x = 1, y= 10;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.