473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

cleanup and rethrow

I haven't used try/catch/finally very much in Javascript. My function
(let's call it try_it()) needs to call a function that could throw an
exception (let's call it dangerous()) with some setup() beforehand and
cleanup() afterwards. What I want to make sure cleanup() is called
whether or not dangerous throws an exception, and if it does throw an
exception, rethrow the exception to whatever is calling try_it().

In C++ this is much easier because you can make setup/cleanup a
constructor/destructor pair and the compiler takes care of it whether
or not you have an exception.

I'm not quite sure how to do this; the following seems to work, with
the only downside being that I have to include two references to
cleanup(). Is there a more elegant way that would use finally or
something?

function try_it(x)
{
var rv;
setup();
try {
rv = dangerous(x);
} catch (e)
{
cleanup();
throw(e);
}
cleanup();
return rv;
}

Nov 16 '06 #1
1 4550

Jason S wrote:
I haven't used try/catch/finally very much in Javascript. My function
(let's call it try_it()) needs to call a function that could throw an
exception (let's call it dangerous()) with some setup() beforehand and
cleanup() afterwards. What I want to make sure cleanup() is called
whether or not dangerous throws an exception, and if it does throw an
exception, rethrow the exception to whatever is calling try_it().

In C++ this is much easier because you can make setup/cleanup a
constructor/destructor pair and the compiler takes care of it whether
or not you have an exception.

I'm not quite sure how to do this; the following seems to work, with
the only downside being that I have to include two references to
cleanup(). Is there a more elegant way that would use finally or
something?

function try_it(x)
{
var rv;
setup();
try {
rv = dangerous(x);
} catch (e)
{
cleanup();
throw(e);
}
cleanup();
return rv;
}

Then you want finally:

try {
dangerous();
} catch(ex) {
handle(ex);
throw(ex);
} finally {
cleanup();
}
test:

try {
dangerous();
} catch(ex) {
alert('caught');
throw(ex);
} finally {
alert('cleaning up');
}

finally gets called whether or not an exception occurred, regardless of
whether you re-throw the exception. It basically always gets called no
matter what.

Nov 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: use dmgass at hotmail dot com | last post by:
I'm writing a module and when it is imported by a script I want some code automatically executed when the importing script is finished executing. I'd like it to execute before interactive mode is...
5
by: A | last post by:
Hi, I'm having some difficulty understanding the semantics of the rethrow keyword. Consider the following code: int main(){ try{ ... } catch(SomeException &SE){
8
by: Taylor | last post by:
I've run in to code with this pattern: try { // do some potentially bad stuff } catch(System.Exception ex) { throw ex; }
6
by: cmay | last post by:
I'm just looking for some other opinions on this. I have a control that does employee lookups, auto complete of the name etc. Let say that this control is:...
4
by: Maxwell | last post by:
Hello, Newbie question here for disposing of unmanaged resources in MC++.NET. I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged C++ dll. What I am trying to figure out is...
24
by: Chameleon | last post by:
Is there a possibility to create memory leak, the code below if I run the line: --------------------------------------------------------- MyClass cl = new MyClass();...
69
by: MQ | last post by:
Hi all I am just wondering how most people implement cleanup in C functions. In particular, if the function opens a number of resources, these need to be released properly should an error occur...
8
by: reuce-google | last post by:
Hi folks, I want to store an exception and rethrow it later: CException m_pEc = NULL; // Class variable. try { throw new CMemoryException(); }
4
by: IanWright | last post by:
I've got a section of a program that I can't quite get to work. I'm fairly sure its something very simple/trivial but it looks correct to me, so if someone could help me fix the problem, and explain...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.