The below blog has objective questions on Exception Handling. It concentrates on usage of keywords like throws, throw, try/catch and Exception
This blog is a extension to previous blog of Core Java Objective Question Test 1
1.
2.
3.
4.
5.
6.
7.
This blog is a extension to previous blog of Core Java Objective Question Test 1
1.
1. public class Test { 2. public static void main(String[] args) { 3. System.out.println("hi"); 4. try { 5. System.out.println("Here"); 6. int j=1/0; 7. throw new UserNotFoundException(); 8. 9. } catch (UserNotFoundException e) { 10. System.out.println("Caught"); 11. e.printStackTrace(); 12. } finally{ 13. System.out.println("in finally"); 14. } 15. System.out.println("After catch"); 16. } 17. }
1. hi Here Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionTurorial.Test.main(Test.java:12) in finally 2. hi Here Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionTurorial.Test.main(Test.java:12) in finally After catch 3. hi here caught in finally After catch
2.
package ExceptionTurorial; public class UserNotFoundException{} package ExceptionTurorial; import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) { System.out.println("hi"); throw new UserNotFoundException(); } }
1. Error at line throw new UserNotFoundException() due to no try catch provided. 2. Error at line throw new UserNotFoundException() since UserNotFoundException is not extending the Exception class
3.
package ExceptionTurorial; public class UserNotFoundException extends Exception{} package ExceptionTurorial; public class Test { public static void main(String[] args) { System.out.println("hi"); try { System.out.println("Hi"); } catch (UserNotFoundException e) { e.printStackTrace(); } } }
1. No Error. Prints Hi 2. Error at catching UserNotFoundException as no such Exception is thwon in try block
4.
1. package ExceptionTurorial; 2. public class Test { 3. public static void main(String[] args) { 4. System.out.println("hi"); 5. throw new UserNotFoundException(); 6. try { 7. System.out.println("Hi"); 8. } catch (UserNotFoundException e) { 9. e.printStackTrace(); 10. } 11. } 12. }
1. No Error. Prints Hi 2. Error at line 5. Since UserNotFoundException() is not catched
5.
1. package ExceptionTurorial; 2. public class Test { 3. public static void main(String[] args) { 4. System.out.println("hi"); 5. try { 6. System.out.println("Here"); 7. int j = 1 / 0; 8. 9. throw new UserNotFoundException(); 10. 11. } catch (Exception e) { 12. System.out.println("Caught"); 13. e.printStackTrace(); 14. } finally { 15. System.out.println("in finally"); 16. } 17. System.out.println("After catch"); 18. } 19. }
1. Error on line 9. As No UserNotFoundException is caught 2. hi Here java.lang.ArithmeticException: / by zero at ExceptionTurorial.Test.main(Test.java:9) Caught in finally After catch
6.
1. package ExceptionTurorial; 2. public class Test { 3. public static void main(String[] args) { 4. System.out.println("hi"); 5. try { 6. System.out.println("Here"); 7. int j=1/0; 8. throw new UserNotFoundException(); 9. }catch (UserNotFoundException e) { 10. System.out.println("Caught UserNotFoundException"); 11. e.printStackTrace(); 12. }catch (ArithmeticException e) { 13. System.out.println("Caught ArithmeticException"); 14. e.printStackTrace(); 15. } 16. finally{ 17. System.out.println("in finally"); 18. } 19. System.out.println("After catch"); 20. } 21. }
hi Here java.lang.ArithmeticException: / by zero at ExceptionTurorial.Test.main(Test.java:10) Caught ArithmeticException in finally After catch Option 2 hi Here java.lang.ArithmeticException: / by zero at ExceptionTurorial.Test.main(Test.java:10) Caught ArithmeticException in finally
7.
1. package ExceptionTurorial; 2. public class Test { 3. 4. public static void main(String[] args) { 5. System.out.println("hi"); 6. try { 7. System.out.println("Here"); 8. int j = 1 / 0; 9. throw new UserNotFoundException(); 10. 11. } catch (Exception e) { 12. System.out.println("Caught"); 13. e.printStackTrace(); 14. } catch (UserNotFoundException e) { 15. System.out.println("Caught"); 16. e.printStackTrace(); 17. } finally { 18. System.out.println("in finally"); 19. } 20. System.out.println("After catch"); 21. } 22. 23. }
1. hi Here java.lang.ArithmeticException: / by zero at ExceptionTurorial.Test.main(Test.java:9) Caught in finally After catch 2. Error at line 14. UserNotFoundException already caught at catch block of Exception