Finally in IE

  • Thread starter Arnaud Diederen
  • Start date
A

Arnaud Diederen

Hello,

I've been looking in the archives but found no interesting answer to
my problem.
I was wondering why the following code does not work in IE 5; I'd
expect an alert showing 'In finally!', but I keep getting an IE error
dialog..
Has anyone any idea why that is so?
(the code works in mozilla :) )

Best regards,
Arnaud


----snip----
<html>
<head>
<title>Finally?!</title>
</head>
<body>
<script>
try {

throw "Error!!";

} finally {

alert ("In finally!");
}
</script>
</body>
</html>
----snip----
 
D

Danny@Kendal

Arnaud Diederen (aundro) said:
Hello,

I've been looking in the archives but found no interesting answer to
my problem.
I was wondering why the following code does not work in IE 5; I'd
expect an alert showing 'In finally!', but I keep getting an IE error
dialog..
Has anyone any idea why that is so?

This any help? I tried your code and got "Exception thrown and not caught"
so I Googled on that phrase and found this link.

http://msdn.microsoft.com/library/d...56/html/js56jserrexceptionthrownnotcaught.asp
 
A

Arnaud Diederen

Danny@Kendal said:
This any help? I tried your code and got "Exception thrown and not caught"
so I Googled on that phrase and found this link.

http://msdn.microsoft.com/library/d...56/html/js56jserrexceptionthrownnotcaught.asp

Thank you for the link, it made me try the following:

----snip----
try {
try {

throw "Error!!";

} finally {

document.write ("finally executed");
}

} catch (e) {

alert (e);

} finally {

alert ('last finally');
}
----snip----

... and it works as expected. I must admit I don't really understand
what makes it work, while the other example fail.

Maybe a problem of "using-try/catch/finally-blocks-too-early-before-
the-page-is-fully-loaded" or something..

Thank you for the pointer! :)

Best regards,
Arnaud


--
Arnaud DIEDEREN
Software Developer
IONIC Software
Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
Tel: +32.4.3640364 - Fax: +32.4.2534737
mailto:[email protected]
http://www.ionicsoft.com
 
V

VK

Arnaud said:
try {
try {

throw "Error!!";

} finally {

document.write ("finally executed");
}

} catch (e) {

alert (e);

} finally {

alert ('last finally');
}
----snip----

.. and it works as expected. I must admit I don't really understand
what makes it work, while the other example fail.

throw new Error("My error") is not like some alert("An error happened")
;-)

If a block throws an error it says: "An oops happened which I cannot /
don't want to deal with. Anyone smarter than I am?"

Usually the "smarter partner" has to be in the catch block right here
or in a catch block somewhere higher. If no smart guy (appropriate
catch block) found then the program executes [finally] block and
aborts. Therefore the originally posted testcase is not from the real
life - it is a curiosity puzzle "what if?" In the real programming you
never throw exceptions "into space". There always *must* be a catch
block provided somewhere. Still even a pure curiosity is a good thing
;-) so here how does it work (this is taken from C# but similar to all
C-based languages):

1) If an exception is thrown, each catch clause is inspected in turn
for a type to which the exception can be assigned; be sure to order
them from most specific to least specific
2) when a match is found, the exception object is assigned to the
identifier and the catch statements are executed
3) if no matching catch clause is found, the exception percolates up to
any outer try block that may handle it
4) if a finally clause is included, it's statements are executed after
all other try-catch processing is complete
5) the [finally] clause executes wether or not an exception is thrown
or a break or continue are encountered
Note: If a catch clause invokes System.exit() the finally clause
WILL NOT execute.


So in the case like

try {
throw new Error('My error');
}
funally {
alert('Booh!');
}

Firefox considers your <script> block as self-enclosed system. After
reaching the top (<script> borders) and failing to find an appropriate
catch block, it returns control to [finally] and aborts right after.

Internet Explorer considers your <script> as a part of the global
JScript context. After reaching the top (<script> borders) and failing
to find an appropriate catch block, it forwards control further to the
JScript interpreter in hope that that one knows what to do with it.
Naturally JScript interpreter has not such exception registered in its
table and it does what it was instructed to do in such cases: it aborts
the execution. Therefore the control never gets a chance to come back
to [finally].

Hope it helps.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top