Minimizing the Error (Exception) in Web and Software development

Sometimes we create a code to execute for us. Let’s say, we create a calculator app. In which we have many classes; which include data and methods. Each class has its own specified set of data and methods on which it works.

So, now that you’re having a class of Calculator, and it has some data as value1 and numerous methods. For example you’re having something like a Mathematics class and all of its methods. I won’t use any of the methods that is defined inside a language, because this tutorial is not only for ASP.NET, but is also applicable, to all other programming languages too. Such as Java, C++ and some other Object-oriented languages. I am not aware of PHP, if it applies, do let me know.

@{
  var calc = new Calculator(); // create an calculator class object
  /* variable declaration */
  var val1 = 30;
  var val2 = 15;
  /* expressions and all other activity here... */
  var result = 0;
  result = (val2 * 2) - val1; // we know its gonna be 0
}

Now, lets continue. We have a simpe program, to calculate the result from these 2 provided variable. Please note that this code was for those, who are using ASP.NET, for those you might be using Java the above code would be changed to the following:

public class fileName {
  public void main () {
    Calculator calc = new Calculator(); // create an calculator class object
    /* variable declaration */
    int val1 = 30;
    int val2 = 15;
    /* expressions and all other activity here... */
    int result = 0;
    result = (val2 * 2) - val1; // we know its gonna be 0
  }
}

Ummm, why hate C++? Here is the code for that too:

int main () {
  Calculator calc = new Calculator(); // create an calculator class object
  /* variable declaration */
  int val1 = 30;
  int val2 = 15;
  /* expressions and all other activity here... */
  int result = ;
  result = (val2 * 2) - val1; // we know its gonna be 0
  return 0;
  /* can use void to prevent the return line */
}

Now, you might have come to know about what was done here. There was a simple subtraction, now the user might want to get another result using a division method. Such as:

var some = 2;
var res = some/result;

Boing! Here you get an error, which is known as Exception in the language of Programming. These are the errors that the are generated on the go. In this case it is:

 System.DivideByZeroException

There are many other Exceptions like this, which are annoying. In Web development, they are shown to users which are annoying and are not that much user-friendly. Secondly, in the Software development, these exceptions can break the operation and close the entire software. Too much annoying for the user, that he might uninstall your app.

To control these, you use a block that is known as try catch block. In the try section you write the code to be tried by the Compiler or the machine and in the Catch block you tell compiler what to do if an exception occurs. For example

try {
  /* all your code here to be tried... */
} catch (Exception e) { /* catch all exceptions.. */
  Response.Write("Following error generated: " + e.Message);
}

This way, you can check if any exception occurs and you can do anything related to it. You can show him an error popup, that might be less annoying or even sometimes helpfull too. You can just say: ‘Sorry, you cannot divide an integer by a zero’, instead of ‘Exception detailed CSxxxx: System.DivideByZeroException, blah and blah and blah’.

Try some other codes and tell me where I might be missing some information.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s