Use Server.Transfer in a try-catch

G

Guest

Hello
I'm trying to use a Server.Transfer in a try-catch (I cannot put it outside the Try-Catch as it is nested deep within a component that is called in a try-catch loop)

The problem is that the Server.Transfer always throws the ThreadAbortException. MSDN acknowledges that this is a unque exception that will be automatically rethrown - i.e. it can't be swallowed. Does anyone know if there is extar code I can write (maybe something in the threading namespace) that effectively swallows this so it doesn't rethrow

For example

Tr
Tr
Server.Transfer("ServB.aspx"
Catch ex As System.Threading.ThreadAbortExceptio
'want to kill thread so it doesn't rethrow to "Second Exception handler
Catch ex As Exceptio
Throw e
End Tr
Catch ex As Exceptio
'Second Exception handle
strEx = ex.Messag
End Tr

Thanks
Mark
 
G

Guest

Hey Alex
Nice suggestion, I tried it and it doesn't work - the exception is still thrown

Mar

----- AlexS wrote: ----

Hi, Mar

did you try to use simple return;

Does this prevent further exceptions

Ale

Mark said:
Hello
I'm trying to use a Server.Transfer in a try-catch (I cannot put i
outside the Try-Catch as it is nested deep within a component that is calle
in a try-catch loop)ThreadAbortException. MSDN acknowledges that this is a unque exception tha
will be automatically rethrown - i.e. it can't be swallowed. Does anyon
know if there is extar code I can write (maybe something in the threadin
namespace) that effectively swallows this so it doesn't rethrow
 
A

AlexS

Hi, Mark

did you try to use simple return; ?

Does this prevent further exceptions?

Alex

Mark said:
Hello,
I'm trying to use a Server.Transfer in a try-catch (I cannot put it
outside the Try-Catch as it is nested deep within a component that is called
in a try-catch loop).
The problem is that the Server.Transfer always throws the
ThreadAbortException. MSDN acknowledges that this is a unque exception that
will be automatically rethrown - i.e. it can't be swallowed. Does anyone
know if there is extar code I can write (maybe something in the threading
namespace) that effectively swallows this so it doesn't rethrow?
 
J

Jason DeFontes

Calling Server.Transfer is like calling Exit from an exe. It means you
are all done, and processing of this request should end. If you still
have more code that you want to execute after calling Server.Transfer,
then you need to reorganize your code so that executes first, by, for
example, returning a status code from "deep within" your component, that
tells the calling code to execute the transfer.

-Jason
 
D

DalePres

Try Server.Execute. I don't know if it will help in your exact application
but it may be worth a try.

Dale
 
G

Guest

Hey Jason
I thought that Server.Transfer was like a goto, you transfer (goto) from Page A to Page B
I'm not trying to run any code after the server.transfer on page A, rather I'm trying to stop running all Page A code (in this case the try-catch block) and transfer to page B. The problem is that it keeps bubbling up the ThreadAbortException

Any ideas

Thanks
Mar


----- Jason DeFontes wrote: ----

Calling Server.Transfer is like calling Exit from an exe. It means you
are all done, and processing of this request should end. If you still
have more code that you want to execute after calling Server.Transfer,
then you need to reorganize your code so that executes first, by, for
example, returning a status code from "deep within" your component, that
tells the calling code to execute the transfer

-Jaso

Mark wrote
 
J

Jason DeFontes

This is why Server.Transfer throws the exception in the first place, to
unwind the stack so nothing else gets executed. Just leave it alone and
let it do it's thing.

-Jason
 
G

Guest

The problem is if I just "leave it alone", the exception it throws will be published (global error handler to catch all exceptions and publish them). I am stuck with someone else's architecture that requires me to switch pages with a component that uses Server.Transfer, and therefore an exception will be logged at every new page - which is what I want to avoid

That's why I'm trying to find a way to swallow the ThreadAbortException

Thanks
Mar

----- Jason DeFontes wrote: ----

This is why Server.Transfer throws the exception in the first place, to
unwind the stack so nothing else gets executed. Just leave it alone and
let it do it's thing

-Jaso

Mark wrote
 
J

Jason DeFontes

I think your only option is to ignore the ThreadAbortException in your
logging. Possibly you can inspect the stack trace of the exception and
only ignore it if Server.Transfer is in the stack.

-Jason
 
D

DalePres

Server.Execute is like a goto. It runs the code in the new page and returns
to the original page. In fact, you'll even end up with two sets of header
HTML tags in the client source.

Server.Transfer is more like changing processes.

Dale

Mark said:
Hey Jason,
I thought that Server.Transfer was like a goto, you transfer (goto) from Page A to Page B.
I'm not trying to run any code after the server.transfer on page A, rather
I'm trying to stop running all Page A code (in this case the try-catch
block) and transfer to page B. The problem is that it keeps bubbling up the
ThreadAbortException.
 
Joined
May 25, 2009
Messages
1
Reaction score
0
Hi,

Its not the best thing to do to catch and ignore exceptions as normal programming logic (Exceptions are expensive) but the below code does its job:


Try


Server.Transfer("yourpage.aspx")

Catch ex As ThreadAbortException
'Ignore ThreadAbortException
Catch ex As Exception

'Handle other type of errors


End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top