Precedence of try blocks.

S

Serguei.Goumeniouk

Dear Experts,
I have a strange problem in the following codes:

1 try {
2 // Some declarations
3 try {
4 // Some codes, which run out of memory
5 } catch (Exception ex) {
6 // Printout A
7 }
8 } catch (OutOfMemoryError ex) {
9 // Printout B
10 }

My program runs out of memory inside the inner try/catch block (at line
#4). In this case IMHO it should be the printout A to be activated, but
in reality I have the printout B. Does this behavior violate the
precedence of try blocks?
Regards,
Serguei.
 
C

Chris Uppal

My program runs out of memory inside the inner try/catch block (at line
#4). In this case IMHO it should be the printout A to be activated, but
in reality I have the printout B. Does this behavior violate the
precedence of try blocks?

No. OutOfMemoryError is not a subclass of Exception (it inherits from
java.lang.Error, not java.lang.Exception) so your inner try block doesn't catch
that error.

-- chris
 
G

Gordon Beaton

My program runs out of memory inside the inner try/catch block (at
line #4). In this case IMHO it should be the printout A to be
activated, but in reality I have the printout B.

OutOfMemoryError is a subclass of Error (not Exception), so it isn't
caught by the inner catch.

/gordon
 
R

Roedy Green

1 try {
2 // Some declarations
3 try {
4 // Some codes, which run out of memory
5 } catch (Exception ex) {
6 // Printout A
7 }
8 } catch (OutOfMemoryError ex) {
9 // Printout B
10 }

My program runs out of memory inside the inner try/catch block (at line
#4). In this case IMHO it should be the printout A to be activated, but
in reality I have the printout B. Does this behavior violate the
precedence of try blocks?
Regards,
Serguei.

OutOfMemoryError is a Throwable but is is not an Exception. It is an
Error. See http://mindprod.com/jgloss/exeception.html
 

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
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top