exception-handling icon indicating copy to clipboard operation
exception-handling copied to clipboard

rethrow semantics question

Open yamt opened this issue 1 year ago • 7 comments

i'm reading https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md#rethrowing-an-exception how the following should work was not clear to me.

try
catch
  try
    try 
    catch
      rethrow 2
    end
  catch ;; can this catch the rethrown exception?
  end
  ;; or, does the above rethrow work as if it was "rethrow 0" here?
end

yamt avatar Feb 17 '23 14:02 yamt

i'm reading https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md#rethrowing-an-exception how the following should work was not clear to me.

try
catch x <---- the exception that this catch caught is rethrown below
  try
    try 
    catch
      rethrow 2
    end
  catch x ;; can this catch the rethrown exception? --> yes, if the tag indices match
  end
  ;; or, does the above rethrow work as if it was "rethrow 0" here? --> no
end

rethrow n throws from its position, basically this searches the caught exception a val* in the (n+1)th surrounding block, (here the 3rd) then pushes the caught exception's values val* onto the stack and throws the tag address a.

There is a detailed example of how this works in "formal-examples" document.

ioannad avatar Feb 17 '23 17:02 ioannad

This example may help understanding: https://github.com/WebAssembly/exception-handling/issues/146#issuecomment-777714491

aheejin avatar Feb 17 '23 23:02 aheejin

thank you. so, in the following, N=1 and N=0 refer the same exception?

try
  throw $tag1
catch_all
  try
    try
      throw $tag2
    catch_all
      rethrow 2
    end
  catch_all
    rethrow N
  end
end

yamt avatar Feb 18 '23 00:02 yamt

Added the example to the explainer too: #263

aheejin avatar Feb 18 '23 00:02 aheejin

thank you. so, in the following, N=1 and N=0 refer the same exception?

No.

try
  throw $tag1
catch_all      ;; N == 1 will rethrow the exception caught by this
  try
    try
      throw $tag2
    catch_all
      rethrow 2
    end
  catch_all    ;; N == 0 will rethrow the exception caught by this
    rethrow N
  end
end

aheejin avatar Feb 18 '23 00:02 aheejin

thank you. so, in the following, N=1 and N=0 refer the same exception?

No.

try
  throw $tag1
catch_all      ;; N == 1 will rethrow the exception caught by this
  try
    try
      throw $tag2
    catch_all
      rethrow 2
    end
  catch_all    ;; N == 0 will rethrow the exception caught by this
    rethrow N
  end
end

i don't understand.

if N==0,

  1. "rethrow 2" rethrows the exception caught by the first catch_all
  2. the exception is caught by the third catch_all
  3. "rethrow N" rethrows the exception, which is the same exception "rethrow 2" rethrown.

if N==1,

  1. same
  2. same
  3. "rethrow N" rethrows the exception caught by the first catch_all

so, in both cases, the exception thrown by "rethrow N" is the exception caught by the first catch_all.

what am i missing?

yamt avatar Feb 18 '23 01:02 yamt

Oh, you are talking about the program as a whole... Yeah, the whole program ends up throwing the same exception for N == 0 and N == 1. What I was describing was independent instruction's behavior.

aheejin avatar Feb 18 '23 01:02 aheejin