Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Easy <cftry> question

Explorer ,
Jul 28, 2008 Jul 28, 2008
This should be an easy question.

When writing a <cftry> statement, is it ok to abort within the cfcatch/cftry statement or is that a no no for writing code?

Abort within catch statement:

<cftry>
<cfquery name="">
</cfquery>

<cfcatch type="any">
<cfinclude template="index.cfm">
<cfabort>
</cfcatch>
</cftry>

This is the code I have been using but maybe I don't need to do this:

<cfset foundDatabaseError = "No">

<cftry>
<cfquery name="">
</cfquery>

<cfcatch type="any">
<cfset foundDatabaseError = "Yes">
</cfcatch>
</cftry>

<cfif foundDatabaseError equal "Yes">
<cfinclude template="index.cfm">
<cfabort>
</cfif>

My thought was to close the <cftry> statement before jumping to another page but maybe that is not necessary.

Comments?

476
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 28, 2008 Jul 28, 2008
It would be better to cflocate to index.cfm in your cftry block instead of including the template and aborting.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 29, 2008 Jul 29, 2008
LATEST
Dan,

Maybe you can explain something to me. When I tried using <cflocation> as you suggested, my errors don't show up. They only show up when using <cfinclude> so that's why I use that particular statement.

This doesn't work:

<cfset foundDatabaseError = "No">

<cftry>
<cfquery name="">
</cfquery>

<cfcatch type="any">
<cfset foundDatabaseError = "Yes">
</cfcatch>
</cftry>

<cfif foundDatabaseError equal "Yes">
<cfset session.errors[1] = "Database Connection error.">
<cflocation url="index.cfm">
<cfabort>
</cfif>

This works:

<cfset foundDatabaseError = "No">

<cftry>
<cfquery name="">
</cfquery>

<cfcatch type="any">
<cfset foundDatabaseError = "Yes">
</cfcatch>
</cftry>

<cfif foundDatabaseError equal "Yes">
<cfset session.errors[1] = "Database Connection error.">
<cfinclude template="index.cfm">
<cfabort>
</cfif>

Application.cfm

some start code
.
.
.
<cfif not IsArray("session.errors")>
<cfset session.errors = ArrayNew(1)>
</cfif>
.
.
some end code


Index.cfm

some start code
.
.
.
<cfoutput>
<cfif not ArrayIsEmpty(session.errors)>
<cfloop index="loopCounter" from="1" to="#ArrayLen(session.errors)#">
<tr>
<td id="red">#session.errors[loopCounter]#</td>
</tr>
</cfloop>
<cfset clearErrors = ArrayClear(session.errors)>
<cfelse>
<tr>
<td> </td>
</tr>
</cfif>
</cfoutput>
.
.
.
some end code

I've tried the above code several times and the session.errors will only show up when doing a <cfinclude> statement and not a <cflocation> statement. Why is that?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 28, 2008 Jul 28, 2008
> It would be better to cflocate to index.cfm in your cftry block instead of including the template and aborting.

It depends on the situation. I would say Dan's position is a bit
"blanket".

Most of our processing code is done within CFC methods, so usually we throw
(or rethrow) an exception after whatever try/catch processing need needed
has taken place; then leave it to the calling (UI) code to decide whether
the exception can be ignored or not. The UI handling of this might simply
be the default error template (which presents the message part of the
error, but skinned according to the website, for example).

Sometimes the best thing to have happen when a trapped exception occurs is
to pretty much halt processing, because - after all - the code that threw
the exception was there for a reason, and it's often not appropriate to
simply carry on as if it didn't have a problem.

To give you the short answer to your actual question: There is no problem
using <cfabort> in these circumstances, provided it's the best handling of
the situation. Although I'm not necessarily sure it's often the best way
of handling it. There's plenty of situations in our code that we do this
very thing though.

--
Adam
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources