开发者

Try/Catch - back to original try

Let's look at this simple try/catch example...

try
{
    // User inputs path of a file
    if(!ValidPat开发者_运维问答h)
        throw new InvalidPathException();
}
catch InvalidPathException e
{
    // Log error
    // Re-throw the error.
    throw;     
}

Couple questions. Both are probably simple answers.

  1. Does the throw in the catch go back and check the next catch, and then the next, and so on?
  2. Is there a way to go back to the original try? Or is that bad programming practice?


  1. Yes the throw statement in the catch block will be re-catches by any wrapper/next catch block.

  2. It is a bad programming practice to modify the program flow using Basic Goto: like statements so it is not advisable to return back to the originating code block (which would also be impractical). Also you always try to handle exceptions close to where they occur since debugger output & stack trace will be much more informative that way.


Each try block can have multiple catches, but only one will be used. It will catch the closest exception. If you then throw the exception, it will leave this try block completely and only a surrounding catch will be able to handle a rethrown exception.


1.Does the throw in the catch go back and check the next catch, and then the next, and so on?

Only the first catch block that matches the exception in for any try block will be chosen. If a catch block rethrows the exception, then it is propogated up to an outer try/catch block if any, or up the call stack to any other try/catch blocks in the call stack. (It will not be caught by another catch block in the same try/catch block.)

2.Is there a way to go back to the original try? Or is that bad programming practice?

In C# you cannot return to the location where the exception was thrown, if that is what you are asking. If you would like to do the entire try block over again, then you just put it in a loop. This requires that you do not rethrow the exception, though, because that would send the execution out of the method.

See the answer to this question for an example of retrying the try block.


  1. Does the throw in the catch go back and check the next catch, and then the next, and so on?

    Ans: No, it does not. Only catch block only.

  2. Is there a way to go back to the original try? Or is that bad programming practice?

    Ans: Yes, few different tactics/hacks.

Ref 1: Try-catch every line of code without individual try-catch blocks

Example:

public delegate void VoidDelegate();

public static class Utils
{
  public static void Try(VoidDelegate v) {
    try {
      v();
    }
    catch {}
  }
}

Utils.Try( () => WidgetMaker.SetAlignment(57) );
Utils.Try( () => arrayname["Title"] = txtTitle.Text );
Utils.Try( () => objectname.Season(true, false) );
Utils.Try( () => (Session["CasseroleTracker"]).Seasoned = true );

another way is to use GOTO statement:

Ref 2: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/2294d2ce-e6ae-4152-b022-61aa2ae9372d/

Another way is with On Error Resume Next

Ref 3: http://msdn.microsoft.com/en-us/library/5hsw66as.aspx


var anyway = true;
while(anyway)
{
try
{
do somthing
...
...
...
if no errors anyway = false
}
catch
{
   somthing wrong!
}
}

;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜