- 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
Is it possible to throw exception without using "throws Exception" in java?
When an exception occurs in Java, the program terminates abnormally and the code past the line that caused the exception doesn’t get executed.
To resolve this you need to either wrap the code that causes the exception within try catch ot, throw the exception using the throws clause. If you throw the exception using throws clause it will be p[postponed to the calling line i.e.
Example
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ExceptionExample{ public static String readFile(String path)throws FileNotFoundException { String data = null; Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); sb.append(sc.next()); data = sb.toString(); return data; } public static void main(String args[]) { String path = "E://test//sample.txt"; readFile(path); } }
Output
Compile-time error
ExceptionExample.java:17: error: unreported exception FileNotFoundException; must be caught or declared to be thrown readFile(path); ^ 1 error
Without using throws
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). If you re-throw the exception, just like in the case of throws clause this exception now, will be generated at in the method that calls the current one.
Example
In the following Java example our code in demo method() might throw ArrayIndexOutOfBoundsException an ArithmeticException. We are catching these two exceptions in two different catch blocks.
In the catch blocks, we are re-throwing both exceptions one by wrapping within the higher exception and the other one directly.
import java.util.Arrays; import java.util.Scanner; public class RethrowExample { public void demoMethod() { Scanner sc = new Scanner(System.in); int[] arr = {10, 20, 30, 2, 0, 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) { throw new IndexOutOfBoundsException(); } catch(ArithmeticException e) { throw e; } } public static void main(String [] args) { new RethrowExample().demoMethod(); } }
Output1
Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 0 4 Exception in thread "main" java.lang.ArithmeticException: / by zero at myPackage.RethrowExample.demoMethod(RethrowExample.java:16) at myPackage.RethrowExample.main(RethrowExample.java:25)
Output2
Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 124 5 Exception in thread "main" java.lang.IndexOutOfBoundsException at myPackage.RethrowExample.demoMethod(RethrowExample.java:17) at myPackage.RethrowExample.main(RethrowExample.java:23)
- Related Articles
- Is it possible to create a custom exception in java without extending Exception class?
- Is it possible to resume java execution after exception occurs?
- How do you throw an Exception without breaking a for loop in java?
- While chaining, can we throw unchecked exception from a checked exception in java?
- Using “TYPE = InnoDB” in MySQL throws an exception?
- Can a constructor throw an exception in Java?
- Throw Custom Exception in Kotlin
- How to throw a C++ exception?
- How to throw custom exception in Kotlin?
- toDataURL throw Uncaught Security exception in HTML
- \nHow to throw an exception from a static block in Java? \n
- What is the difference between throw e and throw new Exception(e) in catch block in java?
- Throw and throws in Java
- If a method in parent class “throws Exception”, can we remove it in overridden method in java?
- Guidelines to follow in while overriding a method that throws an exception in java?
