Convert checked to unchecked

Convert checked to unchecked

Medium Java Exceptions 19 views
Explanation Complexity

Problem Statement

Task: wrap IOException into RuntimeException and rethrow.

Input Format

Code that may throw IOException.

Output Format

If IOException occurs → wrap it into RuntimeException and rethrow.

Example

Reading a file that does not exist
RuntimeException

Constraints

• Must catch IOException

• Must wrap it into RuntimeException

• Must rethrow

Concept Explanation

IOException is a checked exception.
Sometimes we convert it into RuntimeException
so caller is not forced to handle it.

Step-by-Step Explanation

1.Write code inside try block.

2.Catch IOException.

3.Inside catch block:

• Create new RuntimeException(e).

• Throw it.

Concept Explanation

IOException is a checked exception.
Sometimes we convert it into RuntimeException
so caller is not forced to handle it.

Step-by-Step Explanation

1.Write code inside try block.

2.Catch IOException.

3.Inside catch block:

• Create new RuntimeException(e).

• Throw it.

Input / Output Format

Input Format
Code that may throw IOException.
Output Format
If IOException occurs → wrap it into RuntimeException and rethrow.
Constraints
• Must catch IOException

• Must wrap it into RuntimeException

• Must rethrow

Examples

Input:
Reading a file that does not exist
Output:
RuntimeException

Example Solution (Public)

Java
static int rethrowIO(java.io.IOException e){throw new RuntimeException(e);}

Official Solution Code

static int rethrowIO(java.io.IOException e){throw new RuntimeException(e);}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.