If you are the only one touching the code, do you need try/except blocks? [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionI'm building a social networking website. I'm the only coder on it. Right now, Im' the only one touching the code.
Assuming I know what the users' will input into the website, I catch them all with IF ELSE
...do I really need try/excepts?
I went through my code and realize that I have almost 0 try/except blocks...because I catch all the cases with IF/ELSE...However, I do log every error (and sends me an email when there's an error, which never happens)
I've done a lot of user testing and can't find any bugs.
开发者_如何学CI feel like I'm doing something wrong because I don't have any try/excepts in my code.
Using try/catch blocks isn't required (well...depending upon language, which you didn't specify), and certainly isn't related to how many people are touching the code. Generally speaking, however, you should never assume that you know everything that a user might input into your website (unless perhaps you have implemented a very restrictive Filter
/equivalent construct that discards anything that does not match an expected pattern).
In fact, in my experience being the only person working on and testing the code makes it much more difficult to discover issues because you know exactly how the code works and what it is expecting, so you subconsciously feed it values that you know it can handle. Usually as soon as you get another person to test it/try and break it they will come up with a scenario that you didn't think of which breaks the system in about 5 minutes or so.
Anyways, if the accepted way of dealing with unexpected input in your implementation language/framework/paradigm is to use try/catch blocks, then it follows that you should have some. If not, then don't worry about it. The correctness of a piece of code is not measured by which language constructs it uses and which it does not.
What you are saying implies that
1) you have written a perfect code which can not encounter any error at all. 2) you are not using any api which throws exception which you need to handle in your code.
If that is the case then you are good to assume you don't need error handling. Apart from that using error handling is good for unseen cases like no sql connection available when you are trying to do some sql execution using jdbc, malformed input, etc..
精彩评论