Smeggles Smeggles - 1 year ago 75
C# Question

What is the purpose of using try, catch blocks?

Is it a replacement for if, then blocks? I have seen a lot of code where they are used like that.

Answer Source

No, it is not a replacement for an if, then block, it serves an entirely different purpose. The objective of a try, catch block is to try and do something which could fail and raise an exception (e.g., read a file from disk, but the file might not be there, etc.). After catching an exception, you can handle it.

try {
catch (ExpectedException) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download