Connecting Tech Pros Worldwide Forums | Help | Site Map

Use Server.Transfer in a try-catch

Mark
Guest
 
Posts: n/a
#1: Nov 18 '05
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
AlexS
Guest
 
Posts: n/a
#2: Nov 18 '05

re: Use Server.Transfer in a try-catch


Hi, Mark

did you try to use simple return; ?

Does this prevent further exceptions?

Alex

"Mark" <anonymous@discussions.microsoft.com> wrote in message
news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com...[color=blue]
> Hello,
> I'm trying to use a Server.Transfer in a try-catch (I cannot put it[/color]
outside the Try-Catch as it is nested deep within a component that is called
in a try-catch loop).[color=blue]
>
> The problem is that the Server.Transfer always throws the[/color]
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?[color=blue]
>
> For example:
>
> Try
> Try
> Server.Transfer("ServB.aspx")
> Catch ex As System.Threading.ThreadAbortException
> 'want to kill thread so it doesn't rethrow to "Second[/color]
Exception handler"[color=blue]
> Catch ex As Exception
> Throw ex
> End Try
> Catch ex As Exception
> 'Second Exception handler
> strEx = ex.Message
> End Try
>
> Thanks,
> Mark[/color]


Mark
Guest
 
Posts: n/a
#3: Nov 18 '05

re: Use Server.Transfer in a try-catch


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" <anonymous@discussions.microsoft.com> wrote in messag
news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com..[color=blue]
> Hello
> I'm trying to use a Server.Transfer in a try-catch (I cannot put i[/color]
outside the Try-Catch as it is nested deep within a component that is calle
in a try-catch loop)[color=blue][color=green]
>> The problem is that the Server.Transfer always throws th[/color][/color]
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[color=blue][color=green]
>> For example
>> Tr[/color]
> Tr
> Server.Transfer("ServB.aspx"
> Catch ex As System.Threading.ThreadAbortExceptio
> 'want to kill thread so it doesn't rethrow to "Secon[/color]
Exception handler[color=blue]
> Catch ex As Exceptio
> Throw e
> End Tr
> Catch ex As Exceptio
> 'Second Exception handle
> strEx = ex.Messag
> End Tr[color=green]
>> Thanks[/color]
> Mar[/color]



Jason DeFontes
Guest
 
Posts: n/a
#4: Nov 18 '05

re: Use Server.Transfer in a try-catch


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

Mark wrote:
[color=blue]
> Hey Alex,
> Nice suggestion, I tried it and it doesn't work - the exception is still thrown.
>
> Mark
>
> ----- AlexS wrote: -----
>
> Hi, Mark
>
> did you try to use simple return; ?
>
> Does this prevent further exceptions?
>
> Alex
>
> "Mark" <anonymous@discussions.microsoft.com> wrote in message
> news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com...[color=green]
> > Hello,
> > I'm trying to use a Server.Transfer in a try-catch (I cannot put it[/color]
> outside the Try-Catch as it is nested deep within a component that is called
> in a try-catch loop).[color=green][color=darkred]
> >> The problem is that the Server.Transfer always throws the[/color][/color]
> 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?[color=green][color=darkred]
> >> For example:
> >> Try[/color]
> > Try
> > Server.Transfer("ServB.aspx")
> > Catch ex As System.Threading.ThreadAbortException
> > 'want to kill thread so it doesn't rethrow to "Second[/color]
> Exception handler"[color=green]
> > Catch ex As Exception
> > Throw ex
> > End Try
> > Catch ex As Exception
> > 'Second Exception handler
> > strEx = ex.Message
> > End Try[color=darkred]
> >> Thanks,[/color]
> > Mark[/color]
>
>
>[/color]
DalePres
Guest
 
Posts: n/a
#5: Nov 18 '05

re: Use Server.Transfer in a try-catch


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

Dale

"Jason DeFontes" <jason@defontes.com> wrote in message
news:uxJ$bjVOEHA.3596@tk2msftngp13.phx.gbl...[color=blue]
> 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
>
> Mark wrote:
>[color=green]
> > Hey Alex,
> > Nice suggestion, I tried it and it doesn't work - the exception is[/color][/color]
still thrown.[color=blue][color=green]
> >
> > Mark
> >
> > ----- AlexS wrote: -----
> >
> > Hi, Mark
> >
> > did you try to use simple return; ?
> >
> > Does this prevent further exceptions?
> >
> > Alex
> >
> > "Mark" <anonymous@discussions.microsoft.com> wrote in message
> > news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com...[color=darkred]
> > > Hello,
> > > I'm trying to use a Server.Transfer in a try-catch (I cannot[/color][/color][/color]
put it[color=blue][color=green]
> > outside the Try-Catch as it is nested deep within a component that[/color][/color]
is called[color=blue][color=green]
> > in a try-catch loop).[color=darkred]
> > >> The problem is that the Server.Transfer always throws the[/color]
> > ThreadAbortException. MSDN acknowledges that this is a unque[/color][/color]
exception that[color=blue][color=green]
> > will be automatically rethrown - i.e. it can't be swallowed. Does[/color][/color]
anyone[color=blue][color=green]
> > know if there is extar code I can write (maybe something in the[/color][/color]
threading[color=blue][color=green]
> > namespace) that effectively swallows this so it doesn't rethrow?[color=darkred]
> > >> For example:
> > >> Try
> > > Try
> > > Server.Transfer("ServB.aspx")
> > > Catch ex As System.Threading.ThreadAbortException
> > > 'want to kill thread so it doesn't rethrow to[/color][/color][/color]
"Second[color=blue][color=green]
> > Exception handler"[color=darkred]
> > > Catch ex As Exception
> > > Throw ex
> > > End Try
> > > Catch ex As Exception
> > > 'Second Exception handler
> > > strEx = ex.Message
> > > End Try
> > >> Thanks,
> > > Mark[/color]
> >
> >
> >[/color][/color]


Mark
Guest
 
Posts: n/a
#6: Nov 18 '05

re: Use Server.Transfer in a try-catch


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
[color=blue]
> Hey Alex
> Nice suggestion, I tried it and it doesn't work - the exception is still thrown[color=green]
>> Mar
>> ----- AlexS wrote: ----
>> Hi, Mar
>> did you try to use simple return;
>> Does this prevent further exceptions
>> Ale
>> "Mark" <anonymous@discussions.microsoft.com> wrote in messag[/color]
> news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com..[color=green]
>> Hello
>> I'm trying to use a Server.Transfer in a try-catch (I cannot put i[/color]
> outside the Try-Catch as it is nested deep within a component that is calle
> in a try-catch loop)[color=green][color=darkred]
>>> The problem is that the Server.Transfer always throws th[/color][/color]
> 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[color=green][color=darkred]
>>> For example
>>> Tr[/color]
>> Tr
>> Server.Transfer("ServB.aspx"
>> Catch ex As System.Threading.ThreadAbortExceptio
>> 'want to kill thread so it doesn't rethrow to "Secon[/color]
> Exception handler[color=green]
>> Catch ex As Exceptio
>> Throw e
>> End Tr
>> Catch ex As Exceptio
>> 'Second Exception handle
>> strEx = ex.Messag
>> End Tr[color=darkred]
>>> Thanks[/color]
>> Mar[color=darkred]
>>>[/color][/color][/color]
Jason DeFontes
Guest
 
Posts: n/a
#7: Nov 18 '05

re: Use Server.Transfer in a try-catch


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

Mark wrote:[color=blue]
> 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,
> Mark
>
>
> ----- 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.
>
> -Jason
>
> Mark wrote:
>[color=green]
> > Hey Alex,
> > Nice suggestion, I tried it and it doesn't work - the exception is still thrown.[color=darkred]
> >> Mark
> >> ----- AlexS wrote: -----
> >> Hi, Mark
> >> did you try to use simple return; ?
> >> Does this prevent further exceptions?
> >> Alex
> >> "Mark" <anonymous@discussions.microsoft.com> wrote in message[/color]
> > news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com...[color=darkred]
> >> Hello,
> >> I'm trying to use a Server.Transfer in a try-catch (I cannot put it[/color]
> > outside the Try-Catch as it is nested deep within a component that is called
> > in a try-catch loop).[color=darkred]
> >>> The problem is that the Server.Transfer always throws the[/color]
> > 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?[color=darkred]
> >>> For example:
> >>> Try
> >> Try
> >> Server.Transfer("ServB.aspx")
> >> Catch ex As System.Threading.ThreadAbortException
> >> 'want to kill thread so it doesn't rethrow to "Second[/color]
> > Exception handler"[color=darkred]
> >> Catch ex As Exception
> >> Throw ex
> >> End Try
> >> Catch ex As Exception
> >> 'Second Exception handler
> >> strEx = ex.Message
> >> End Try
> >>> Thanks,
> >> Mark
> >>>[/color][/color][/color]
Mark
Guest
 
Posts: n/a
#8: Nov 18 '05

re: Use Server.Transfer in a try-catch


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[color=blue]
> 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[color=green]
>> Any ideas
>> Thanks[/color]
> Mar[color=green][color=darkred]
>>> ----- Jason DeFontes wrote: ----[/color]
>> Calling Server.Transfer is like calling Exit from an exe. It means you[/color]
> 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[color=green]
>> -Jaso
>> Mark wrote[color=darkred]
>>> Hey Alex[/color]
>> Nice suggestion, I tried it and it doesn't work - the exception is still thrown[color=darkred]
>>> Mar
>>> ----- AlexS wrote: ----
>>> Hi, Mar
>>> did you try to use simple return;
>>> Does this prevent further exceptions
>>> Ale
>>> "Mark" <anonymous@discussions.microsoft.com> wrote in messag[/color]
>> news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com..[color=darkred]
>>> Hello
>>> I'm trying to use a Server.Transfer in a try-catch (I cannot put i[/color]
>> outside the Try-Catch as it is nested deep within a component that is calle
>> in a try-catch loop)[color=darkred]
>>>> The problem is that the Server.Transfer always throws th[/color]
>> 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[color=darkred]
>>>> For example
>>>> Tr
>>> Tr
>>> Server.Transfer("ServB.aspx"
>>> Catch ex As System.Threading.ThreadAbortExceptio
>>> 'want to kill thread so it doesn't rethrow to "Secon[/color]
>> Exception handler[color=darkred]
>>> Catch ex As Exceptio
>>> Throw e
>>> End Tr
>>> Catch ex As Exceptio
>>> 'Second Exception handle
>>> strEx = ex.Messag
>>> End Tr
>>>> Thanks
>>> Mar
>>>>[/color][/color][/color]
Jason DeFontes
Guest
 
Posts: n/a
#9: Nov 18 '05

re: Use Server.Transfer in a try-catch


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

Mark wrote:
[color=blue]
> 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,
> Mark
>
> ----- 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.
>
> -Jason
>
> Mark wrote:[color=green]
> > 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.[color=darkred]
> >> Any ideas?
> >> Thanks,[/color]
> > Mark[color=darkred]
> >>> ----- Jason DeFontes wrote: -----
> >> Calling Server.Transfer is like calling Exit from an exe. It means you[/color]
> > 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.[color=darkred]
> >> -Jason
> >> Mark wrote:
> >>> Hey Alex,
> >> Nice suggestion, I tried it and it doesn't work - the exception is still thrown.
> >>> Mark
> >>> ----- AlexS wrote: -----
> >>> Hi, Mark
> >>> did you try to use simple return; ?
> >>> Does this prevent further exceptions?
> >>> Alex
> >>> "Mark" <anonymous@discussions.microsoft.com> wrote in message
> >> news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com...
> >>> 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:
> >>>> Try
> >>> Try
> >>> Server.Transfer("ServB.aspx")
> >>> Catch ex As System.Threading.ThreadAbortException
> >>> 'want to kill thread so it doesn't rethrow to "Second
> >> Exception handler"
> >>> Catch ex As Exception
> >>> Throw ex
> >>> End Try
> >>> Catch ex As Exception
> >>> 'Second Exception handler
> >>> strEx = ex.Message
> >>> End Try
> >>>> Thanks,
> >>> Mark
> >>>>[/color][/color][/color]
DalePres
Guest
 
Posts: n/a
#10: Nov 18 '05

re: Use Server.Transfer in a try-catch


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" <anonymous@discussions.microsoft.com> wrote in message
news:0CFA4A41-6DF9-43FD-B6F7-5E0F6E6D01F8@microsoft.com...[color=blue]
> Hey Jason,
> I thought that Server.Transfer was like a goto, you transfer (goto) from[/color]
Page A to Page B.[color=blue]
> I'm not trying to run any code after the server.transfer on page A, rather[/color]
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.[color=blue]
>
> Any ideas?
>
> Thanks,
> Mark
>
>
> ----- Jason DeFontes wrote: -----
>
> Calling Server.Transfer is like calling Exit from an exe. It means[/color]
you[color=blue]
> are all done, and processing of this request should end. If you still
> have more code that you want to execute after calling[/color]
Server.Transfer,[color=blue]
> then you need to reorganize your code so that executes first, by, for
> example, returning a status code from "deep within" your component,[/color]
that[color=blue]
> tells the calling code to execute the transfer.
>
> -Jason
>
> Mark wrote:
>[color=green]
> > Hey Alex,
> > Nice suggestion, I tried it and it doesn't work - the exception[/color][/color]
is still thrown.[color=blue][color=green][color=darkred]
> >> Mark
> >> ----- AlexS wrote: -----
> >> Hi, Mark
> >> did you try to use simple return; ?
> >> Does this prevent further exceptions?
> >> Alex
> >> "Mark" <anonymous@discussions.microsoft.com> wrote in message[/color]
> > news:D68D8B79-6742-4491-B1B3-A78A6BF7C4BA@microsoft.com...[color=darkred]
> >> Hello,
> >> I'm trying to use a Server.Transfer in a try-catch (I cannot put[/color][/color][/color]
it[color=blue][color=green]
> > outside the Try-Catch as it is nested deep within a component[/color][/color]
that is called[color=blue][color=green]
> > in a try-catch loop).[color=darkred]
> >>> The problem is that the Server.Transfer always throws the[/color]
> > ThreadAbortException. MSDN acknowledges that this is a unque[/color][/color]
exception that[color=blue][color=green]
> > will be automatically rethrown - i.e. it can't be swallowed.[/color][/color]
Does anyone[color=blue][color=green]
> > know if there is extar code I can write (maybe something in[/color][/color]
the threading[color=blue][color=green]
> > namespace) that effectively swallows this so it doesn't[/color][/color]
rethrow?[color=blue][color=green][color=darkred]
> >>> For example:
> >>> Try
> >> Try
> >> Server.Transfer("ServB.aspx")
> >> Catch ex As System.Threading.ThreadAbortException
> >> 'want to kill thread so it doesn't rethrow to[/color][/color][/color]
"Second[color=blue][color=green]
> > Exception handler"[color=darkred]
> >> Catch ex As Exception
> >> Throw ex
> >> End Try
> >> Catch ex As Exception
> >> 'Second Exception handler
> >> strEx = ex.Message
> >> End Try
> >>> Thanks,
> >> Mark
> >>>[/color][/color][/color]


Closed Thread