Reputation: 1869
I am learnig CSharp.I have some doubts in handling exceptions.Kindly guide me to improve my coding knowledge.
Suppose i construct a code segment :
try {
SomeWork();
return success;
}
catch (someException ex)
{
throw new ExceptionDetails(ex);
return failure;
}
catch(AnotherException exp)
{
throw new ExceptionDetails(exp);
return failure;
}
finally
{
CleanUpStuff();
}
Questions:
(1) Can i use return statement after "throw" (throwing exception) ?
(2) Is throwing an exception ugly practice?.When exactly do i need to throw an exception?Do i need to use "throw" to report only custom exception ?
(3)
try
{
SomeWork();
}
catch(StringIndexOutOfBound ex)
{
throw;
}
using anonymous throw statement inside catch is a good practice?
Upvotes: 3
Views: 303
Reputation: 187030
Whan you throw an exception your return statement won't work.
Try to catch the specific exception and handle it from there itself and also return the value after the finally block.
throw ex
will reset the stack trace to the point of the throw.
throw
will maintain the original stack which will be helpful when debugging.
Upvotes: 1
Reputation: 47873
1) Having a return statement after a thrown Exception will not compile and you should get an Unreachable code warning.
2) Throwing an exception is perfectly valid, this potentially allows for some more generic error handling to occur. e.g.
public void readFile()
{
try
{
// Perform IO action
}
catch ( FileNotFoundException ex )
{
// Perform error recovery e.g. create a default file
}
catch ( Exception ex )
{
// Cant perform any error recovery at this level so throw the exception to allow the calling code to deal with it
throw;
}
}
You could argue here that you should be testing to see if the File exists before perforrming the IO action. As a rule of thumb if you can test for something with an if statement, then you should do so. You can generally catch specific exceptions when you know you can recover from them within the scope that they are generated.
This sort of question really depends on the situation as testing before carring out an action could be a better course of action. You will get more of a feel for what is required with practice!
3) As has been mentioned elsewhere in the post, it is generally a good idea not to just 'throw' an exception as having information about how and where it occurred is often very useful in debugging.
Upvotes: 1
Reputation: 33445
1) A thrown exception (if not handled) will automatically return to the previous method so there is no need to return. In fact, the compiler should warn you that there is unreachable code.
2) Throwing exceptions is not ugly as long as you do it for ... exceptional circumstances. Your coding should be defensive enough that you don't rely on exceptions for normal program flow.
3) Don't do #3 if you're not going to be doing anything else with the exception. If you are going to be logging it or doing something meaningful and would still like to rethrow the exception, then use it "anonymously" (??) as you have there.
Upvotes: 3
Reputation: 29157
In regards to 3- read about the differences between throw and throw ex. As Daniel says the compiler will tell you about unreachable code.
Upvotes: 1
Reputation: 19956
Hint: anything after throw will never be executed. Compiler should tell you that!
Upvotes: 3