Tuesday, March 20, 2018

The beauty of Exception Handling.

I had already studied about exception handling. I knew almost all the theoretical aspect of exception handling(what is it? how it works? what it solves?) and many more. But, had I implemented it to solve any exceptions? No. Had I realized it’s beauty and it’s power? No.
Recently, I had to present my final semester project in college. Everything was working fine till day before today’s morning. Then, something happened to my code which broke my entire project apart. Even though I knew it was my poor programming pattern that broke the project, I couldn’t just change the code and start from scratch. I had to solve what broke my project anyhow. I was in a state of panic and didn’t know what to do. I did what most of us beginners do while debugging (study error tracebacks, search stack overflow extensively and try to fix that error). I did that and nothing changed. Code was still broken. I didn’t know what to do.
Then, suddenly in some forum, I read something like you can use
try:
 # do something
 except:
 # return None
Then it clicked in my mind. Why can’t I use exception handling to solve my problem. I knew where the problem was, I knew what was the problem and it’s root cause. So, I could easily handle it via above method. So, I opened up my project directory along with each and every file. I put the code that was throwing error inside try block and I handled every exception it could throw.
After that, I tried to run my project and it ran successfully without any errors. That time I realized what exactly is exception handling and why it is such an important concept in programming. At that moment, I realized the beauty of exception handling.