Will a program ever crash if the main loop is put in a try/catch block [closed]

If we have a class main and the constructor of main constructs our setupclass and we put this class in a try catch in main.

Theoratically Will all errors go into catch? If they will how will it effect the performance and mantainability? Will the application be crash free?

8

Your program can still crash or otherwise fail to perform it’s tasks:

  • Your exception handling code could have a bug in it; by definition your handler is outside of the code ‘protected’ by your try / catch, so an error there will still result in a program crash.

  • Just because you handled the exception, doesn’t mean you your program state is still sane. Your program could easily end up in a state that is effectively useless, because it’s internal state is left in an inconsistent state. You program will then continually throw exceptions, and the end user will be left with no other option but to terminate your program forcibly.

Moreover, because you catch all exceptions, you’ll have a much harder time debugging what actually goes wrong in your program, and you can never be sure if the exceptions you see are the results of broken internal state after a previous exception was handled, or a genuine bug.

Better to let your program crash. At least then the end user understands the program broke, and you as a developer will have a much better sense of what broke the program.

1

Yes and no. I work on embedded systems. When an unhandled exception occurs we log it and reboot the system, very similar to what you’re suggesting. Maybe 9 times out of 10 the reboot clears up the problem, at least good enough while the exception report is being analyzed for a fix. The other 1 time in 10 we get a continuous reboot cycle, which is enough of a problem unto its own that we keep track of how many reboots have happened and go into a “recovery” mode after a few tries.

At any rate, it’s no panacea. It’s a small bandage you apply to give you time to fix it for real, and it’s only helpful if you get those exception reports and have solid initialization code that truly resets the state of your system.

Depends on languaga but probably not.

In c# then other threads generating exceptions will kill the main process off the top of my head.

It’s irrelevant though, except for very simple programs, you’re likely to end up in an irrecoverable state resulting in bad or lost data which for most users is much worse than the application crashing.

3

The appropriate goal is not to make your application “crash free” but to make your application reliable and stable.

It may be possible to reduce or even virtually eliminate crashes with exception handling. However, all other things being equal, this will simply replace crashes with cases in which the program enters an unusable state.

This isn’t going to look any better to your users. In fact, it will very likely be worse. At least a crash is a clear, clean failure. A program in a non-functioning state is ambiguous at best. The user may waste a long time to see if the application is frozen or just “thinking”. At worst it may actually do damage, continuing to perform actions, but in a corrupt manner.

There is no shortcut to writing good, reliable 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