How to Anticipate Exceptions in C# When There Are No Checked Exceptions

93 views Asked by At

In Java, calls to functions that can throw Exceptions need to anticipate them with either a try-catch or by adding a throws Exception to their function signature.

My main use for them is in my top-calling code, where I only put try-catches in sections that explicitly throw Exceptions.

However, because C# does not have checked exceptions, the only way I can think of anticipating Exceptions is to surround all sections of my top-calling code with try-catches. I personally do not like this approach for the following reasons:

  1. It can lead to very messy code, putting try-catches where they are not needed.

  2. I might miss a section that needs a try-catch.

How do I properly anticipate Exceptions in my top-calling code in C#?

0

There are 0 answers