Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are given the follow class declarations: class RecognitionError extends Exce

ID: 3795003 • Letter: Y

Question

You are given the follow class declarations: class RecognitionError extends Exception {...} class MismatchedTokenError extends RecognitionError {...} class InvalidTokenError extends RecognitionError {...} class NoAvailableAlternativeError extends RecognitionError {...} class MismatchedNumberError extends MismatchedTokenError {...} class MismatchedOperatorError extends MismatchedTokenError {...} For each of the following Java code snippets, explain what is wrong with the snippet and how you would fix it. try {} catch (RecognitionError re) {} catch (InvalidTokenError te) {} catch (MismatchedOperatorError oe) {} b. public void evaluate () {if (error condition) {throw new InvalidTokenError()}}

Explanation / Answer

a.

Problem with thegiven code snippet is:

Unreachable catch block for InvalidTokenError . It is already handled by the catch block for RecognitionError.

and

Unreachable catch block for MismatchedOperatorError. It is already handled by the catch block for InvalidTokenError.

Catch child exceptions first and then parent exceptions. This can be fixed by below code snippet:

try {
           ...

       }
       catch(Mismatchedoperatorerror oe) {
           ...
       }
       catch(InvalidTokenError te) {
           ...
       }
       catch(RecognitionError re) {
           ...
       }

b.

For general error conditions,throw most generic error, so that all other exceptions can be handled.

This can be fixed ny beow code snippet:

public void evaluate()

{

...

if(error condition){

throw new RocognitionError();

}

...

}

Please comment for any queries/feedbacks.

Thanks.

.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote