Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  try catch blocks scope  (Read 1181 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Devvie





« Posted 2006-04-13 11:56:10 »

Hello.
If you put something inside try block, does it apply on everything inside try block? Like if you open new blocks or loops inside, does the try / catch apply to them also? Kind of silly question, but I'm debuging and I'm little bit confused about output to console. Thank you.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-04-13 12:47:16 »

Scope is between { and }

1  
2  
3  
4  
5  
6  
7  
{
   int a = 6;

   { // A is still accessible in here
       int b = 5;
   }  // B is not accessible anymore
} // A is not accessible anymore


so the try/catch code doesn't really matter, only the { and }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline CommanderKeith
« Reply #2 - Posted 2006-04-13 14:22:55 »

When you have a try-catch block inside another, any exception thrown within the nested try will be caught by the nested try's catch and not the outer try's catch unless you re-throw it.

For example, the following will print "caught in nested try" only:

try{
 try{
   throw new Exception()
 }
 catch(Exception e){
  System.out.println("caught in nested try");
 }
}
catch(Exception e){
 System.out.println("caught in outer try");
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Devvie





« Reply #3 - Posted 2006-04-13 17:53:48 »

thank you guys.... Allthough it wasn't a direct anwser it can be figured out from it.

I was actually asking will something like this be cought:

try {
    throw new Exception() // - this will be cougth
    while (something) {
        throw new Exception() // - wasnt sure about this since it's in a new block
    }
}

It's clear now that both will be cought. Thanks.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2006-04-13 18:04:22 »

Direct answers are bad

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2006-04-13 22:11:43 »

thank you guys.... Allthough it wasn't a direct anwser it can be figured out from it.

I was actually asking will something like this be cought:

try {
    throw new Exception() // - this will be cougth
    while (something) {
        throw new Exception() // - wasnt sure about this since it's in a new block
    }
}

You will never reach the while as the first throw will jump you out to the catch block.

In fact, this shouldnt even compile as you have provably unreachable code....

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline kevglass

« JGO Spiffy Duke »


Medals: 208
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2006-04-13 22:15:31 »

You know, I have a feeling it was a contrived example to illustrate a point Wink

Kev

Offline Jeff

JGO Coder




Got any cats?


« Reply #7 - Posted 2006-04-14 23:10:37 »

You know, I have a feeling it was a contrived example to illustrate a point Wink

Kev

Maybe, but even so he asked "what does this do."

Thats the answer.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (67 views)
2014-11-26 15:22:04

toopeicgaming1999 (58 views)
2014-11-26 15:20:36

toopeicgaming1999 (13 views)
2014-11-26 15:20:08

SHC (26 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (30 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (53 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!