Is it possible to have multiple tries, with only one catch The 2019 Stack Overflow Developer Survey Results Are InMultiple or Single Try CatchCatch multiple exceptions at once?Exception thrown inside catch block - will it be caught again?Including all the jars in a directory within the Java classpathThe case against checked exceptionsWhy does Java have transient fields?How can I catch all the exceptions that will be thrown through reading and writing a file?Proper way to declare custom exceptions in modern Python?Can I catch multiple Java exceptions in the same catch clause?Catch multiple exceptions in one line (except block)Which part of throwing an Exception is expensive?
What is the motivation for a law requiring 2 parties to consent for recording a conversation
What is the meaning of Triage in Cybersec world?
Does HR tell a hiring manager about salary negotiations?
Ubuntu Server install with full GUI
How did passengers keep warm on sail ships?
Why didn't the Event Horizon Telescope team mention Sagittarius A*?
Keeping a retro style to sci-fi spaceships?
How do I free up internal storage if I don't have any apps downloaded?
Is bread bad for ducks?
Why doesn't UInt have a toDouble()?
Can we generate random numbers using irrational numbers like π and e?
Loose spokes after only a few rides
Correct punctuation for showing a character's confusion
Does adding complexity mean a more secure cipher?
Can withdrawing asylum be illegal?
Geography at the pixel level
Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?
If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
Why can't devices on different VLANs, but on the same subnet, communicate?
Output the Arecibo Message
How to charge AirPods to keep battery healthy?
Can a flute soloist sit?
How can I have a shield and a way of attacking with a ranged weapon at the same time?
Is it possible to have multiple tries, with only one catch
The 2019 Stack Overflow Developer Survey Results Are InMultiple or Single Try CatchCatch multiple exceptions at once?Exception thrown inside catch block - will it be caught again?Including all the jars in a directory within the Java classpathThe case against checked exceptionsWhy does Java have transient fields?How can I catch all the exceptions that will be thrown through reading and writing a file?Proper way to declare custom exceptions in modern Python?Can I catch multiple Java exceptions in the same catch clause?Catch multiple exceptions in one line (except block)Which part of throwing an Exception is expensive?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm adding multiple players to a team and each individual player needs a try catch, but the catch is always the same
try
team.addPlayer( new Player( 1, "PLYR1" ) );
catch ( Exception e )
System.out.println( e.toString() );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
Is there any way to simplify this?
java exception
add a comment |
I'm adding multiple players to a team and each individual player needs a try catch, but the catch is always the same
try
team.addPlayer( new Player( 1, "PLYR1" ) );
catch ( Exception e )
System.out.println( e.toString() );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
Is there any way to simplify this?
java exception
3
You can add the players in the sametry
clause, and have only onecatch
.
– Maroun
Mar 8 at 11:08
3
The answer is yes, but the exact way you would do it depends on what behaviour you want. For example, if adding player 1 fails do you want to try adding player 2 or do you want to stop?
– Steve Bosman
Mar 8 at 11:12
Possible duplicate of Multiple or Single Try Catch
– Googlian
Mar 8 at 11:14
add a comment |
I'm adding multiple players to a team and each individual player needs a try catch, but the catch is always the same
try
team.addPlayer( new Player( 1, "PLYR1" ) );
catch ( Exception e )
System.out.println( e.toString() );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
Is there any way to simplify this?
java exception
I'm adding multiple players to a team and each individual player needs a try catch, but the catch is always the same
try
team.addPlayer( new Player( 1, "PLYR1" ) );
catch ( Exception e )
System.out.println( e.toString() );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
Is there any way to simplify this?
java exception
java exception
edited Mar 8 at 11:08
Maroun
74.8k19146207
74.8k19146207
asked Mar 8 at 11:06
CrazineXCrazineX
1
1
3
You can add the players in the sametry
clause, and have only onecatch
.
– Maroun
Mar 8 at 11:08
3
The answer is yes, but the exact way you would do it depends on what behaviour you want. For example, if adding player 1 fails do you want to try adding player 2 or do you want to stop?
– Steve Bosman
Mar 8 at 11:12
Possible duplicate of Multiple or Single Try Catch
– Googlian
Mar 8 at 11:14
add a comment |
3
You can add the players in the sametry
clause, and have only onecatch
.
– Maroun
Mar 8 at 11:08
3
The answer is yes, but the exact way you would do it depends on what behaviour you want. For example, if adding player 1 fails do you want to try adding player 2 or do you want to stop?
– Steve Bosman
Mar 8 at 11:12
Possible duplicate of Multiple or Single Try Catch
– Googlian
Mar 8 at 11:14
3
3
You can add the players in the same
try
clause, and have only one catch
.– Maroun
Mar 8 at 11:08
You can add the players in the same
try
clause, and have only one catch
.– Maroun
Mar 8 at 11:08
3
3
The answer is yes, but the exact way you would do it depends on what behaviour you want. For example, if adding player 1 fails do you want to try adding player 2 or do you want to stop?
– Steve Bosman
Mar 8 at 11:12
The answer is yes, but the exact way you would do it depends on what behaviour you want. For example, if adding player 1 fails do you want to try adding player 2 or do you want to stop?
– Steve Bosman
Mar 8 at 11:12
Possible duplicate of Multiple or Single Try Catch
– Googlian
Mar 8 at 11:14
Possible duplicate of Multiple or Single Try Catch
– Googlian
Mar 8 at 11:14
add a comment |
3 Answers
3
active
oldest
votes
as saying at your comments you can use this
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
of if you care about if the first player is added, use this:
try
team.addPlayer( new Player( 1, "PLYR1" ) );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
you can try this code instead of using two try blocks.
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
If you want to iterate through all possible players
using catch and throw the same error to stdout
if an exception occurs you should use a basic for loop.
Player
class is assumed to be defined and team
object created:
String[] players = "PLYR1", "PLYR2", "PLYR3"
for (String player: players)
try
team.addPlayer( new Player( 1, player ) );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55061919%2fis-it-possible-to-have-multiple-tries-with-only-one-catch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
as saying at your comments you can use this
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
of if you care about if the first player is added, use this:
try
team.addPlayer( new Player( 1, "PLYR1" ) );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
as saying at your comments you can use this
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
of if you care about if the first player is added, use this:
try
team.addPlayer( new Player( 1, "PLYR1" ) );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
as saying at your comments you can use this
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
of if you care about if the first player is added, use this:
try
team.addPlayer( new Player( 1, "PLYR1" ) );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
catch ( Exception e )
System.out.println( e.toString() );
as saying at your comments you can use this
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
of if you care about if the first player is added, use this:
try
team.addPlayer( new Player( 1, "PLYR1" ) );
try
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
catch ( Exception e )
System.out.println( e.toString() );
answered Mar 8 at 11:16
Christos ThemelisChristos Themelis
1044
1044
add a comment |
add a comment |
you can try this code instead of using two try blocks.
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
you can try this code instead of using two try blocks.
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
you can try this code instead of using two try blocks.
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
you can try this code instead of using two try blocks.
try
team.addPlayer( new Player( 1, "PLYR1" ) );
team.addPlayer( new Player( 2, "PLYR2" ) );
catch ( Exception e )
System.out.println( e.toString() );
answered Mar 8 at 11:21
pavithrapavithra
111
111
add a comment |
add a comment |
If you want to iterate through all possible players
using catch and throw the same error to stdout
if an exception occurs you should use a basic for loop.
Player
class is assumed to be defined and team
object created:
String[] players = "PLYR1", "PLYR2", "PLYR3"
for (String player: players)
try
team.addPlayer( new Player( 1, player ) );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
If you want to iterate through all possible players
using catch and throw the same error to stdout
if an exception occurs you should use a basic for loop.
Player
class is assumed to be defined and team
object created:
String[] players = "PLYR1", "PLYR2", "PLYR3"
for (String player: players)
try
team.addPlayer( new Player( 1, player ) );
catch ( Exception e )
System.out.println( e.toString() );
add a comment |
If you want to iterate through all possible players
using catch and throw the same error to stdout
if an exception occurs you should use a basic for loop.
Player
class is assumed to be defined and team
object created:
String[] players = "PLYR1", "PLYR2", "PLYR3"
for (String player: players)
try
team.addPlayer( new Player( 1, player ) );
catch ( Exception e )
System.out.println( e.toString() );
If you want to iterate through all possible players
using catch and throw the same error to stdout
if an exception occurs you should use a basic for loop.
Player
class is assumed to be defined and team
object created:
String[] players = "PLYR1", "PLYR2", "PLYR3"
for (String player: players)
try
team.addPlayer( new Player( 1, player ) );
catch ( Exception e )
System.out.println( e.toString() );
answered Mar 8 at 11:53
Szymon MaszkeSzymon Maszke
2,725724
2,725724
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55061919%2fis-it-possible-to-have-multiple-tries-with-only-one-catch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
You can add the players in the same
try
clause, and have only onecatch
.– Maroun
Mar 8 at 11:08
3
The answer is yes, but the exact way you would do it depends on what behaviour you want. For example, if adding player 1 fails do you want to try adding player 2 or do you want to stop?
– Steve Bosman
Mar 8 at 11:12
Possible duplicate of Multiple or Single Try Catch
– Googlian
Mar 8 at 11:14