Reputation: 151
I want to catch the exception threw at different steps. Look at the code below. Let us say each function Procedure1
, Procedure2
,... ,Procedure6
may throw various kinds of exceptions (may overlap!). And if exception happened at step 1-3, I want to throw newly-defined exception ex1
and step 4-6 ex2
.
public void TestFunc(){
try{
var res1 = Procedure1();
var res2 = Procedure2(res1);
var res3 = Procedure3(res2);
var res4 = Procedure4(res3);
var res5 = Procedure5(res4);
Procedure6(res5);
}
catch{
// if error/exception occurs in step 1-3 throw exception 1
}
catch{
// if error/exception occurs in step 4-6 throw exception 2
}
}
I have read https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch but really did not get the point.
If all steps throw different exceptions, then I can catch different ones. But the exceptions for different steps may overlap. So I cannot really tell which step it is from the exception message. (Yeah, this is can be overcome by adding extra info on exception messages, but again I really do not want to modify other functions.)
One way I can think of is to break the TestFunc
into two piece, but again I really do not want to do this as it breaks the integrity of the function.
And if we break the steps into two try
, the editor shows the message res3 does not exist in the context
. This is reasonable as {}
separates the context.
public void TestFunc(){
try{
var res1 = Procedure1();
var res2 = Procedure2(res1);
var res3 = Procedure3(res2);
}
catch{
// if error in step 1-3 throw exception 1
}
try{
var res4 = Procedure4(res3); // error, res3 cannot be found in the context
var res5 = Procedure5(res4);
Procedure6(res5);
}
catch{
// if error in step 4-6 throw exception 2
}
}
Upvotes: 2
Views: 64
Reputation: 42320
When you start writing code with this level of error handling, it necessarily gets very verbose, and most of your code ends up handling the error cases. You should embrace that.
One way to do it is as you already described:
Res1 res1;
try
{
res1 = Procedure1();
}
catch ....
Res2 res2;
try
{
res2 = Procedure2(res1);
}
catch ....
Alternatively, you can write the procedures in their own helper methods which handles their error handling:
private Res1 HandleProcecure1()
{
try
{
return Procedure1();
}
catch ....
}
private Res2 HandleProcecure2(Res1 res1)
{
try
{
return Procedure2(res1);
}
catch ....
}
...
var res1 = HandleProcedure1();
var res2 = HandleProcedure2(res1);
Unless there are methods which need exactly the same exception handling, personally I prefer not to try and group them. Give each its own set catch
statements, and write some helper methods which mean you avoid writing the same things inside each of them.
Upvotes: 0
Reputation: 186813
I suggest nested construction
try {
var res1 = Procedure1();
var res2 = Procedure2(res1);
var res3 = Procedure3(res2);
try {
var res4 = Procedure4(res3);
var res5 = Procedure5(res4);
Procedure6(res5);
}
catch {
// Specific exceptions if error/exception occurs in step 4-6
}
}
catch {
// Exceptions within res1...res3
// Unspecific Exceptions within steps 4-6
}
Upvotes: 3
Reputation: 37460
Just declare variables outside try
:
public void TestFunc(){
// declare all variables here
ResultType res1, res2, res3, res4, res5;
try{
res1 = Procedure1();
res2 = Procedure2(res1);
res3 = Procedure3(res2);
}
catch{
// if error in step 1-3 throw exception 1
}
try{
res4 = Procedure4(res3); // error, res3 cannot be found in the context
res5 = Procedure5(res4);
Procedure6(res5);
}
catch{
// if error in step 4-6 throw exception 2
}
}
Upvotes: 0