How do I return a value on try catch?

How do I return a value on try catch?

To return a value when using try/catch you can use a temporary variable, e.g. Else you need to have a return in every execution path (try block or catch block) that has no throw .

Can we return a value in catch block?

Yes, we can write a return statement of the method in catch and finally block. If we return a value in the catch block and we can return a value at the end of the method, the code will execute successfully.

How do you know which exception to catch?

Here are some ideas:

  1. Dig through manually. At least you will know some of the exceptions.
  2. Use reflection to find any throw statements accessible from doSomething .
  3. Run your test cases and log the exceptions thrown like above.
  4. Go to the people who put the catch there in the first place.

Can you return in a Catch statement?

Return statement in catch will be executed only if the catch block is reached, i.e. if there is an error thrown. example() will return 2 since an error was thrown before return 1 . But if there is a finally block and this finally block has a return statement then this return will override catch return statement.

What happens if catch and finally both return value?

When catch and finally block both return value, method will ultimately return value returned by finally block irrespective of value returned by catch block. When try and finally block both return value, method will ultimately return value returned by finally block irrespective of value returned by try block.

What will happen if a return in try block?

If try block fails (exception occurs), control transfers to the catch block where the exception is handled. The remaining code in the try block is never executed. If try/catch blocks have a return statement, even then the finally block executes!

What happens if I write return in try block?

Upon the completion of finally block execution, control goes back to the return statement in the try block and returns “returning from try block”. If finally block has a return statement, then the return statements from try/catch blocks will be overridden.

Can we return in finally block?

Yes you can write the return statement in a finally block and it will override the other return value. The output is always 2, as we are returning 2 from the finally block.

What happens when catch and finally both return value?

Can not leave the body of a finally clause?

CodeRush Classic shows the Control cannot leave the body of a finally clause code issue if a finally block includes statements redirecting the program execution flow out of this block (e.g. return, goto, continue).

When do you need to catch the expected exception?

If you need to catch the expected exception (i.e., to assert certain details, like the message / properties on the exception), it’s important to catch the specific expected type, and not the base Exception class.

How to test exceptions with Assert and catch?

As an aside, when debugging unit tests which throw exceptions, you may want to prevent VS from breaking on the exception. Just to give an example of Matthew’s comment below, the return of the generic Assert.Throws and Assert.Catch is the exception with the type of the exception, which you can then examine for further inspection:

When to return from method in catch block?

So you caught an exception, proceeded it… and returned from a method. If the caller method does not care about the exception, this is absolutely normal. You can add a return in the catch block. You explicitly know that your code will return and continue execution instead of halting at the catch block.

How to return a value from try, catch, and finally?

Since there could be an error, sum might not get initialized, so put your return statement in the finally block, that way it will for sure be returned. Make sure that you initialize sum outside the try/catch/finally so that it is in scope. Here is another example that return’s a boolean value using try/catch.