What should happen when code is reached that shouldn’t be, according to business or logic rules? [duplicate]

I am currently writing an application that isn’t huge but will be used through-out my company of 200+ people and external customers about projects we are working on (not software projects), but there are a fair amount of business rules, along with the implied ones that go with creating, verifying, and maintaining user accounts.

Semi-often I find myself writing code like this

If (A)
{
   foo();
}
else If (B)
{
   foo2();
}
else
{
   throw new exception("Should not happen because XYZ");
}

or

foo()
{
   //do stuff    
   if (D == F)
   {
       throw new exception("Method should not be called if D==F, you forgot how to use this method");
   }    
   //do other stuff
}

Is this code-smell because it implies that my code might break?

Is this good a programming practice for debugging when things WILL go wrong?

Is this being paranoid?

1

It all depends on how important not clobbering things when someone makes a mistake, or when SNAFU happens, is. I’m going to assume that’s important.

For example, you give an example in foo:

throw new exception("Method should not be called if D==F, you forgot how to use this method");

Assuming that foo doesn’t set D and F, and the programmer does, and of course that foo isn’t supposed to handle the situation where D and F are equal, it’s probably the right choice to throw an error. Programmers are idiots, especially the one who uses your code next.

As for checking that something was successful, that’s not a bad idea if you are able to identify possible points of failure. For example, assuming a HTTP grabber that returns a (integer flag, data object) tuple for whatever reason:

(flag, page_data) = http_fetch(bleh);
if (flag not in ACCEPTABLE_FLAGS) {
    throw new exception("Today is not a good day.");
}
file_thing.write(page_data.bytes);

Here, the error checking will mean that nobody knocks down your door because the internet went down and as a result your code clobbered the databases with random junk.

If it’s your own work, e.g., that you wrote http_fetch above – do the statements anyway. Unless you’re in a really terrible, terrible work environment, people should appreciate that you are just recognising that sometimes the worst case scenario happens.

On the other hand, it’s probably excessive to do the following:

mid = (low + high)/2;
assert((low <= mid) && (mid <= high));

In short, if you can identify a point of failure, cover it. Even if it’s your own code, sometimes things go wrong and the assumptions you made when writing that code – such as that the caller would actually read the docs – are incorrect.

If you are handling the case where you don’t expect something to ever happen, then this is one perfect reason to use runtime exceptions. Also, from http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html (if you are using Java, that is), runtime exceptions should be used when the client cannot do anything to work around the issue (as probably is the case you described). If the case you’ve shown occurs, it represents a code bug, which is when runtime exceptions should be thrown.

However, you should probably only do this in few unique or special cases, not in every single method in the system–that would be being paranoid and not trusting your code.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật