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

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...