- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
While chaining, can we throw unchecked exception from a checked exception in java?
When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).
While re-throwing exceptions you can throw the same exception as it is with out adjusting it as −
try { int result = (arr[a])/(arr[b]); System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); }catch(ArithmeticException e) { throw e; }
Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing higher level of exception maintaining the abstraction.
try { int result = (arr[a])/(arr[b]); System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); }catch(ArrayIndexOutOfBoundsException e) { throw new IndexOutOfBoundsException(); }
Throwing unchecked exception from a checked exception
Yes, we can catch compile time exception (checked) and in the catch block we can wrap it with in a run time exception (unchecked) and re-throw it. But since we are re-throwing using checked exception we need to either wrap it inside an implicit try-catch pair or, skip handling it using the throws clause.
Example
In the following Java example we have created a user defined (checked) exception named SampleException.
We are displaying an integer array of 6 elements and letting user to select the position of the two values and dividing the selected numbers. While choosing the position the user my use index value, beyond the length of the array, which causes an ArrayIndexOutOfBoundsException which is unchecked exception.
In the catch block we are re-throwing this object by wrapping it with in the above created Sample exception, which is checked.
import java.util.Arrays; import java.util.Scanner; class SampleException extends Exception { SampleException(String msg){ super(msg); } } public class Rethrow { public void demoMethod() { Scanner sc = new Scanner(System.in); int[] arr = {10, 20, 30, 2, 5, 8}; System.out.println("Array: "+Arrays.toString(arr)); System.out.println("Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)"); int a = sc.nextInt(); int b = sc.nextInt(); try { int result = (arr[a])/(arr[b]); System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); }catch(ArrayIndexOutOfBoundsException e) { try { throw new SampleException("This is a checked exception"); } catch (SampleException e1) { System.out.println("Checked exception in the catch block"); } } } public static void main(String [] args) { new Rethrow().demoMethod(); } }
Output
Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 25 24 Checked exception in the catch block
- Related Articles
- Can we throw an Unchecked Exception from a static block in java?
- Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?
- Difference Between Checked and Unchecked Exception in Java
- How can we decide that custom exception should be checked or unchecked in java?
- Are the instances of Exception checked or unchecked exceptions in java?
- Exception chaining in Java
- Can a constructor throw an exception in Java?
- While overriding can the subclass choose not to throw an exception in java?
- Java Program to Handle Unchecked Exception
- How to create a custom unchecked exception in Java?
- Is it possible to throw exception without using "throws Exception" in java?
- \nHow to throw an exception from a static block in Java? \n
- How can we create a custom exception in Java?
- Throw Custom Exception in Kotlin
- Can the abstract methods of an interface throw an exception in java?
