Let us consider the following C# code as an example:
public static string GetCurentExecutableDirectory()
{
return System.IO.Path.GetFullPath(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
According to documentation, System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
cannot throw any exception, so we can suppose this part reliable. But the part System.IO.Path.GetFullPath
can throw a few exceptions according to documentation.
I know that the code code will work fine 99.99% of the time, but if the execution is critical, I am a bit confused. On the one hand, I don't want to crash even if somehow System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
yields something that would force System.IO.Path.GetFullPath
to raise an exception. On the other hand, I'm not sure checking things that should be evident makes much sense. (And as this example situation itself is nonsensical, the handling is quite delicate.)
The above is just an example and my question should be considered in the general sense
In a nutshell
In a critical piece of code, should exceptions describing a situation which does not makes sense be handled?
Exception
(the ancestor of all exception classes), assuming you don't want your program to crash when it throws one.