I have a method like...
int f() {
try {
int i = process();
return i;
} catch(Exception ex) {
ThrowSpecificFault(ex);
}
}
This produces a compiler error, "not all code paths return a value". But in my case ThrowSpecificFault() will always throw (the appropriate) exception. So I am forced to a put a return value at the end but this is ugly.
The purpose of this pattern in the first place is because "process()" is a call to an external web service but need to translate a variety of different exceptions to match a client's expected interface (~facade pattern I suppose).
Any cleaner way to do this?
How about:
Right now a return type can be a type, or "void" meaning "no return type". We could in theory add a second special return type "never", which has the semantics you want. The end point of an expression statement consisting of a call to a "never" returning method would be considered unreachable, and so it would be legal in every context in C# in which a "goto", "throw" or "return" is legal.
It is highly unlikely that this will be added to the type system now, ten years in. Next time you design a type system from scratch, remember to include a "never" type.
I suppose you could make ThrowSpecificFault return an Object, and then you could
Otherwise, you could rewrite ThrowSpecificFault as a constructor for an Exception subtype, or you could just make ThrowSpecificFault into a factory that creates the exception but doesn't throw it.
No.
Imagine if
ThrowSpecificFault
were defined in a separate DLL. If you modify the DLL to not throw an exception, then run your program without recompiling it, what would happen?The problem here, is that if you go into the
catch
block inf()
your function will never return a value. This will result in an error because you declared your function asint
which means you told the compiler that your method will return an integer.The following code will do what you are looking for and always return an integer.
put the return statement at the end of your function and you will be fine.
It's always a good idea to ensure your method will always return a value no matter what execution path your application goes through.
In you case but that is Your knowledge not the compiler. There is now way to say that this method for sure will throw some nasty exception.
Try this
You can also use the throw key word
But then that method should return some exception instead of throwing it.