Groovy is a dynamic language that simplifies error handling through its robust exception mechanism. Understanding how to use try-catch blocks, create custom exceptions, and apply best practices for handling errors efficiently is crucial for building stable Groovy applications. Below are 30 multiple-choice questions (MCQs) covering these important topics.
try block in Groovy?
try-catch block, what happens if no exceptions are thrown in the try block?
catch blocks can be used in a Groovy try-catch structure?
finally block in Groovy exception handling?
catch block to clean up resourcestry blockcatch blocks in Groovy is correct?
catch block can catch exceptions of any typecatch block must be specific to the exception typecatch blocks are not allowedcatch block is not requiredcatch blockthrow new MyException()raise MyException()catch MyException()throwMyExceptioncatch blockThrowable or ErrorExceptionfinally block if an exception is caught in Groovy?
throw keyword in Groovy?
finally to handle exceptions after the loopthrow erethrow econtinue ereturn ecatch blocks for different exception types?
catch block is executedcatch blockscatch blockscatch blockcatch block?
catch (IOException | SQLException e)catch (IOException and SQLException)catch (IOException, SQLException e)catch (IOException SQLException e)try-catch with an exception variable?
try { // code } catch (Exception e) { // handle e }try { // code } catch (e) { // handle e }try { // code } except (e) { // handle e }try { // code } catch (e: Exception) { // handle e }throw new IllegalArgumentException("Invalid input") do in Groovy?
try-catch blocks in Groovy?
NullPointerException in Groovy?
throwable class?
| Qno | Answer |
|---|---|
| 1 | A) Throwable |
| 2 | C) RuntimeException |
| 3 | A) They must be declared or handled explicitly |
| 4 | C) Both checked and runtime exceptions |
| 5 | A) NullPointerException |
| 6 | B) To execute the code that might throw an exception |
| 7 | B) The finally block is executed |
| 8 | B) Unlimited |
| 9 | B) It runs after the catch block to clean up resources |
| 10 | B) A catch block must be specific to the exception type |
| 11 | B) Exception |
| 12 | C) To handle specific error cases unique to the application |
| 13 | A) throw new MyException() |
| 14 | A) It can extend any exception class |
| 15 | D) IllegalArgumentException |
| 16 | C) Catch specific exceptions to handle errors accurately |
| 17 | B) Clean up resources like closing files or database connections |
| 18 | C) To explicitly throw an exception |
| 19 | A) Catch exceptions inside the loop for each iteration |
| 20 | A) throw e |
| 21 | A) Yes, exceptions can be nested inside each other |
| 22 | A) The first matching catch block is executed |
| 23 | A) `catch (IOException |
| 24 | A) try { // code } catch (Exception e) { // handle e } |
| 25 | C) Throws an exception with a custom message |
| 26 | A) FileNotFoundException |
| 27 | B) To execute code that may raise exceptions and handle them gracefully |
| 28 | A) When the code attempts to access or modify a null object reference |
| 29 | A) It is the base class for all exceptions and errors |
| 30 | A) A message that describes the error |