Normally, catch block are used to handle the exceptions raised in the try block. The exception can re-throw using throw keyword, if catch block is unable to handle it. This process is called as re-throwing an exception.
public class Main { public int test(int n1, int n2) { try{ return n1/n2; }catch(ArithmeticException e){ throw e; } } public static void main(String[] args) { Main main = new Main(); try{ System.out.println(main.test(30, 0)); }catch(Exception e){ e.printStackTrace(); } } } |
Output
java.lang.ArithmeticException: / by zero at Main.test(Main.java:5) at Main.main(Main.java:14) |
Java interview questions on Exception Handling
- what is an exception?
- How the exceptions are handled in java?
- What is the difference between error and exception in java?
- Can we keep other statements in between try catch and finally blocks?
- Explain the exception hierarchy in java?
- What are runtime exceptions in java?
- What is outofmemoryerror in java?
- What are checked and unchecked exceptions in java?
- What is the difference between classnotfoundexception and noclassdeffounderror in java?
- Will finally block get executed if return?
- Can we throw an exception without throws?
- What is rethrowing an exception in java?
- What is the use of throws keyword in java?
- What is exception propagation in java?
- Difference between throw and throws in java?
- What is finally in java?
- What is the difference between final finally and finalize in java?
- How to create customized exceptions in java?
- What is classcastexception in java?
- What is stackoverflowerror in java?
- What is the superclass of all exception classes?
- What is the use of printstacktrace method in java?
- What is arraystoreexception in java?