[CodeComparison]|part 2| try-catch wrapper…

Here is another code comparison that might interest programmers. I will try to document the lessons I learn as I grow as a coder and a programmer. If someone else finds this helpful, be my guest. I will try to be regular but who knows I shift attention to what types of projects…

So here it goes.

Scenario:

I am trying to make a calculator app with inbuilt error handling. I will provide two pieces of code and their output for you to compare which is better. Both codes are exactly the same, the only difference being the position of statements.

This is about playing with try…catch wrappers.

Method1:

Main.java:

package in.cookytech.java;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
       //Declaring the scanner
        Scanner sc = new Scanner(System.in);

        //Input value 1
        double d1 = 0;
        double d2 = 0;
        try {
            System.out.print("Enter value 1: ");
            String s1 = sc.nextLine();
            d1 = Double.parseDouble(s1);

            //Input Value 2
            System.out.print("Enter value 2: ");
            String s2 = sc.nextLine();
            d2 = Double.parseDouble(s2);
        } catch (NumberFormatException e) {
//            e.printStackTrace();
            System.out.println("Input number values please :)");
        }

        //Input the operation
        System.out.print("Enter the operation( +, -, *, / ): ");
        String op = sc.nextLine();

        //SWITCH
        switch (op){
            case "+":
                add(d1, d2);
                break;

            case "-":
                subtract(d1, d2);
                break;

            case "*":
                multiply(d1,d2);
                break;

            case "/":
                divide(d1, d2);
                break;

            default:
                System.out.println("Unknown Operation!");
        }


    }




    static void add(double d1, double d2){
        double result = d1 + d2;
        System.out.println("Sum is: " + result);

    }

    static void subtract(double d1, double d2){
        double result = d1 - d2;
        System.out.println("Difference is: " + result);

    }

    static void multiply(double d1, double d2){
        double result = d1 * d2;
        System.out.println("Product is: " + result);

    }

    static void divide(double d1, double d2){
        if (d2 == 0){
            System.out.println("Division by zero not defined!");
        }else {
            double quot = d1 / d2;
            double rem = d1 % d2;
            System.out.println("Result is: " + quot + "\nRemainder was: " + (int)rem);
        }

    }
}

Code output in the exception case:

Enter value 1: x
Input number values please :)
Enter the operation( +, -, *, / ): *
Product is: 0.0

As you can see, the user will have a bad experience if you are asking input even after an exception has happened. The code will work perfectly well in the normal case but will give irritating and unmanageable results in the exceptional case.

Method2:

I will just rearrange the same lines of code and we will see what difference just a different sequence makes to this exceptional case!

Main.java

package in.cookytech.java;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
       //Declaring the scanner
        Scanner sc = new Scanner(System.in);

        double d1 = 0;
        double d2 = 0;

        try {
            //Input values
            System.out.print("Enter value 1: ");
            String s1 = sc.nextLine();
            System.out.print("Enter value 2: ");
            String s2 = sc.nextLine();
            System.out.print("Enter the operation( +, -, *, / ): ");
            String op = sc.nextLine();

            //parse values
            d1 = Double.parseDouble(s1);
            d2 = Double.parseDouble(s2);

            //SWITCH
            switch (op){
                case "+":
                    add(d1, d2);
                    break;

                case "-":
                    subtract(d1, d2);
                    break;

                case "*":
                    multiply(d1,d2);
                    break;

                case "/":
                    divide(d1, d2);
                    break;

                default:
                    System.out.println("Unknown Operation!");
            }
        } catch (NumberFormatException e) {
//            e.printStackTrace();
            System.out.println("Please enter number values :)\nTry Again!");
        }


    }




    static void add(double d1, double d2){
        double result = d1 + d2;
        System.out.println("Sum is: " + result);

    }

    static void subtract(double d1, double d2){
        double result = d1 - d2;
        System.out.println("Difference is: " + result);

    }

    static void multiply(double d1, double d2){
        double result = d1 * d2;
        System.out.println("Product is: " + result);

    }

    static void divide(double d1, double d2){
        if (d2 == 0){
            System.out.println("Division by zero not defined!");
        }else {
            double quot = d1 / d2;
            double rem = d1 % d2;
            System.out.println("Result is: " + quot + "\nRemainder was: " + (int)rem);
        }
    }
}

The output of the exceptional case:

Enter value 1: x
Enter value 2: 45
Enter the operation( +, -, *, / ): -
Please enter number values :)
Try Again!

Much better user experience IMHO.

Lessons..

We can take a few pointers from this drastic change in user experience:

  • It is not always important to interrupt input exactly where an error has occurred. Sometimes we must give priority to the experience of the users.
  • We should cluster our user input if possible. Both the pieces of code have been well clustered. The first was clustered on the basis of the variable that we were dealing with and the latter on the basis of the kind of operation that we were doing. Both have their pros and cons but on the point of a better user experience, the second logic of clustering makes more sense.

That’s all for today. I hope you agree on the importance of managing the sequence of flow of your program not just to get an output but also to evolve your code into an experience. If you agree, share with those who could find it useful, and leave a comment below!

Leave a Comment

(2 Comments)

  • shail

    Well explanined !!!very useful for those who want to learn coding and even those like me who are not coders…

    • raveesh

      thanks for the compliment

  • Your e-mail address will not be published. Required fields are marked *