"...how can I know the exact thing that fails?"
Asking that question might mean you've got too much going on in the method and it's time to refactor.
Here's your example, copied from your previous question:
void Function1()
{
//Do something...
//If something fail
Throw New Exception("Fails because bla bla");
//Do something...
//If something fail
Throw New Exception("Fails because bla bla bla bla");
//If everything its ok, just continue without throwing anything
}
Function1 is doing a lot. To make maintenance (and testing) easier, you could extract groups of logic into separate methods, each of which returns a bool
value to indicate success.
bool SomethingOne()
{
var successful = true;
//Do something...
//If something fail
successful = false;
return successful;
}
bool SomethingTwo()
{
var successful = true;
//Do something...
//If something fail
successful = false;
return successful;
}
void Function1()
{
var isOneSuccessful = SomethingOne();
if (!isOneSuccessful)
{
// SomethingOne failed - do something
return;
}
var isTwoSuccessful = SomethingTwo();
if (!isTwoSuccessful)
{
// SomethingTwo failed - do something
return;
}
//If everything its ok, just continue without throwing anything
}
YMMV. I'm assuming that "something fails" is a non-critical problem. Like maybe this is a validation routine and you're just going to display a message that says "Please check all fields."
If it's something the programmer using your method needs to be aware of and handle specially, or if it's something the user has to know more about, a simple boolean may not cut it.