Tuesday, April 24, 2018

What is the output of the program ? [based on exception]

  1. class SampleExe {
  2. void run() throws Exception {
  3. System.out.println("run before x");
  4. int x = 10 / 0;
  5. System.out.println("run after x");
  6. }
  7. }

  8. public class SmapleExe1 {
  9. public static void main(String[] args) {
  10. SampleExe exe = new SampleExe();
  11. exe.run();
  12. }
  13. }
Output: Unhandled type exception at compile time at line 12
--------------------------------------------------------------------
  1. class SampleExe {
  2. void run() throws Exception {
  3. System.out.println("run before x");
  4. int x = 10 / 0;
  5. System.out.println("run after x");
  6. }
  7. }

  8. public class SmapleExe1 {
  9. public static void main(String[] args) throws Exception {
  10. SampleExe exe = new SampleExe();
  11. exe.run();
  12. }
  13. }
Output: 
run before x
java.lang.ArithmeticException: / by zero at line 4

--------------------------------------------------------------------
  1. class SampleExe {
  2. void run() throws Exception {
  3. System.out.println("run before x");
  4. int x = 10 / 0;
  5. System.out.println("run after x");
  6. }
  7. }

  8. public class SmapleExe1 {
  9. public static void main(String[] args) {
  10. SampleExe exe = new SampleExe();
  11. try {
  12. exe.run();
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
Output: 
run before x
java.lang.ArithmeticException: / by zero at line 4

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...