• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 00:33
CEST 06:33
KST 13:33
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Team TLMC #5 - Finalists & Open Tournaments0[ASL20] Ro16 Preview Pt2: Turbulence5Classic Games #3: Rogue vs Serral at BlizzCon9[ASL20] Ro16 Preview Pt1: Ascent10Maestros of the Game: Week 1/Play-in Preview12
Community News
Weekly Cups (Sept 8-14): herO & MaxPax split cups3WardiTV TL Team Map Contest #5 Tournaments1SC4ALL $6,000 Open LAN in Philadelphia7Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues29LiuLi Cup - September 2025 Tournaments3
StarCraft 2
General
Team Liquid Map Contest #21 - Presented by Monster Energy #1: Maru - Greatest Players of All Time Weekly Cups (Sept 8-14): herO & MaxPax split cups SpeCial on The Tasteless Podcast Team TLMC #5 - Finalists & Open Tournaments
Tourneys
WardiTV TL Team Map Contest #5 Tournaments Maestros of The Game—$20k event w/ live finals in Paris RSL: Revival, a new crowdfunded tournament series Sparkling Tuna Cup - Weekly Open Tournament SC4ALL $6,000 Open LAN in Philadelphia
Strategy
Custom Maps
External Content
Mutation # 491 Night Drive Mutation # 490 Masters of Midnight Mutation # 489 Bannable Offense Mutation # 488 What Goes Around
Brood War
General
Diplomacy, Cosmonarchy Edition [ASL20] Ro16 Preview Pt2: Turbulence BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ ASL20 General Discussion
Tourneys
[ASL20] Ro16 Group D [ASL20] Ro16 Group C [IPSL] ISPL Season 1 Winter Qualis and Info! Is there English video for group selection for ASL
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile General RTS Discussion Thread Nintendo Switch Thread Borderlands 3
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread Russo-Ukrainian War Thread The Big Programming Thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023
World Cup 2022
Tech Support
Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s)
TL Community
BarCraft in Tokyo Japan for ASL Season5 Final The Automated Ban List
Blogs
The Personality of a Spender…
TrAiDoS
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1339 users

Homework thread

Forum Index > Closed
Post a Reply
1 2 3 4 5 Next All
HeavOnEarth
Profile Blog Joined March 2008
United States7087 Posts
Last Edited: 2009-05-28 00:29:46
May 28 2009 00:28 GMT
#1
Java: Exception Handling + Process Endin's.

1) I'm just having trouble figuring out what's wrong.

2 *If a mod closes this... just ban me from site , christ.
I seriously remember helping people with "homework" countless times - and this isn't even that. This, SHOULD be working, actually, but it isn't.


I have this simple program, with three classes; Starter contains the main, which does nothing but initialize the UserInput class. Inside the UserInput class is where I'm having the problem. Basically, where it throws the exception, it is ending the process, despite catching it. How would I modify this so that the exception gets handled, and the process continues?
public class Starter{

public static void main(String[] args){

UserInput ui = new UserInput();

}
}

And:

public class StringTooLongException extends Exception{

public StringTooLongException(){
}

public String getMessage(){
return "String has more than the max allowed characters.";
}

}

And:

import java.util.Scanner;

public class UserInput{

public UserInput(){

try{
String done = "DONE";
System.out.println("Type a string. Max 20 characters.");
System.out.println("Type DONE, in all caps to end.");
Scanner scan = new Scanner(System.in);
String s = scan.next();
while(s.compareTo(done) != 0){
if(s.length() > 20){
/**
* Here is the problem, it is ending the process,
* when it is obviously catching the exception.
*/
throw new StringTooLongException();
}
System.out.println(s);
s = scan.next();
}
}
catch(StringTooLongException e){
System.out.println(e.getMessage());
System.out.println("Exception caught! Ending program.");
}
}
}


I've tried so many things, and to be honest I'm quite angry -_- , considering I have (basically) the same code in these three classes:

import java.util.Scanner;
public class Starter {

public static void main(String[] args) {

String done = "DONE";
System.out.println("DocType? U = Unclassified, C = Confidential, P = Proprietary.");
System.out.println("Type DONE, in all caps to end.");
Scanner scan = new Scanner(System.in);
String docType = scan.next();
while(docType.compareTo(done) != 0){
Document doc = new Document(docType);
System.out.println(doc);
docType = scan.next();
}
System.out.println("Program Fin~");
}

}

And:

public class Document {

private String dType = "U";
public Document(String docType){
try{
if( docType.compareToIgnoreCase("U") != 0
&& docType.compareToIgnoreCase("C") != 0
&& docType.compareToIgnoreCase("P") != 0 )
throw new InvalidDocumentCodeException();
dType = docType;
}
catch(InvalidDocumentCodeException e){
System.out.println("Exception caught and handled! Continuing...");
System.out.println(e.getMessage());
}
}

public void setDocType(String docType){
dType = docType;
}
public String toString(){
return "Document type: "+ dType.toUpperCase();
}
}

And:

public class InvalidDocumentCodeException extends Exception{

public InvalidDocumentCodeException(){
}

public String getMessage(){
return "Document code must be U (Unclassified), C (Confidential), or P (Proptietary)." +
"\nDocument will default to Unclassified.";
}

}


Thanks muchly! :/
"come korea next time... FXO house... 10 korean, 10 korean"
RaGe
Profile Blog Joined July 2004
Belgium9947 Posts
May 28 2009 00:38 GMT
#2
When an exception gets thrown, it immeadiately skips to the Catch, which is at the end of your program, so it terminates the program cause there's no following lines of code

program is doing what you made it to do lol
Moderatorsometimes I get intimidated by the size of my right testicle
RaGe
Profile Blog Joined July 2004
Belgium9947 Posts
May 28 2009 00:44 GMT
#3
wow i get more and more convinced you have no idea what you're doing and just copy pasted shit or something

i mean, your catch block is:

catch(StringTooLongException e){
System.out.println(e.getMessage());
System.out.println("Exception caught! Ending program.");
}


you even print that you're ending the program and you dont want it to end the program?
Moderatorsometimes I get intimidated by the size of my right testicle
HeavOnEarth
Profile Blog Joined March 2008
United States7087 Posts
May 28 2009 00:49 GMT
#4
On May 28 2009 09:38 RaGe wrote:
When an exception gets thrown, it immeadiately skips to the Catch, which is at the end of your program, so it terminates the program cause there's no following lines of code

program is doing what you made it to do lol


Firstly, the throw is inside a while loop. What I'm wondering is why the crap it isn't continuing the while loop, opposed to what it does now; which is ends the program. It should not be ending the program, it should be throwing an error, catching it, and then going back to the while loop.

On May 28 2009 09:44 RaGe wrote:
wow i get more and more convinced you have no idea what you're doing and just copy pasted shit or something

i mean, your catch block is:

catch(StringTooLongException e){
System.out.println(e.getMessage());
System.out.println("Exception caught! Ending program.");
}


you even print that you're ending the program and you dont want it to end the program?


Oh noes, I has a random print line that hasn't been changed from a previous version. Printing text doesn't end a program. ... (At least not this one)
"come korea next time... FXO house... 10 korean, 10 korean"
Abydos1
Profile Blog Joined October 2008
United States832 Posts
May 28 2009 00:53 GMT
#5
On May 28 2009 09:28 HeavOnEarth wrote:
Show nested quote +

import java.util.Scanner;

public class UserInput{

public UserInput(){

try{
String done = "DONE";
System.out.println("Type a string. Max 20 characters.");
System.out.println("Type DONE, in all caps to end.");
Scanner scan = new Scanner(System.in);
String s = scan.next();
while(s.compareTo(done) != 0){
if(s.length() > 20){
/**
* Here is the problem, it is ending the process,
* when it is obviously catching the exception.
*/
throw new StringTooLongException();
}
System.out.println(s);
s = scan.next();
}
}
catch(StringTooLongException e){
System.out.println(e.getMessage());
System.out.println("Exception caught! Ending program.");
}
}
}



If you're talking about this one the exception keeps going up the stack until it gets caught or the program ends, thus its getting out of the while loop.
"...perhaps the greatest joy possible in Starcraft, being accused of being a maphacker" - Day[9]
RaGe
Profile Blog Joined July 2004
Belgium9947 Posts
May 28 2009 00:54 GMT
#6
Maybe you should stop being so defensive and read the fucking solution for a change.

On May 28 2009 09:38 RaGe wrote:
When an exception gets thrown, it immeadiately skips to the Catch, which is at the end of your program, so it terminates the program cause there's no following lines of code



Your catch block is outside of the while loop, so it doesn't continue in the while loop after it executes the catch block. Java does not magically read your mind. This wasn't supposed to work. This isn't some obscure error. This is you skipping through whatever 'How to handle exceptions' guide you read, copy pasting some stupid shit that you don't understand, and asking tl to fix your errors.
Moderatorsometimes I get intimidated by the size of my right testicle
qrs
Profile Blog Joined December 2007
United States3637 Posts
Last Edited: 2009-05-28 00:56:07
May 28 2009 00:55 GMT
#7
^yes, but your catch block is outside the while loop. It's the same as if you would have used goto in C.
edit: Rage pre-empted me
'As per the American Heart Association, the beat of the Bee Gees song "Stayin' Alive" provides an ideal rhythm in terms of beats per minute to use for hands-only CPR. One can also hum Queen's "Another One Bites The Dust".' —Wikipedia
Malongo
Profile Blog Joined November 2005
Chile3472 Posts
May 28 2009 00:55 GMT
#8
I dont understand this program does exaclty what you want imo. LOL.
Help me! im still improving my English. An eye for an eye makes the whole world blind. M. G.
Malongo
Profile Blog Joined November 2005
Chile3472 Posts
May 28 2009 00:56 GMT
#9
On May 28 2009 09:49 HeavOnEarth wrote:
Show nested quote +
On May 28 2009 09:38 RaGe wrote:
When an exception gets thrown, it immeadiately skips to the Catch, which is at the end of your program, so it terminates the program cause there's no following lines of code

program is doing what you made it to do lol


Firstly, the throw is inside a while loop. What I'm wondering is why the crap it isn't continuing the while loop, opposed to what it does now; which is ends the program. It should not be ending the program, it should be throwing an error, catching it, and then going back to the while loop.

Show nested quote +
On May 28 2009 09:44 RaGe wrote:
wow i get more and more convinced you have no idea what you're doing and just copy pasted shit or something

i mean, your catch block is:

catch(StringTooLongException e){
System.out.println(e.getMessage());
System.out.println("Exception caught! Ending program.");
}


you even print that you're ending the program and you dont want it to end the program?


Oh noes, I has a random print line that hasn't been changed from a previous version. Printing text doesn't end a program. ... (At least not this one)

Catch is outside the loop... -_-
Help me! im still improving my English. An eye for an eye makes the whole world blind. M. G.
micronesia
Profile Blog Joined July 2006
United States24701 Posts
Last Edited: 2009-05-28 01:05:21
May 28 2009 00:57 GMT
#10
I love how you didn't put java or programming or anything like that in the thread title lol

I also like how I have absolutely nothing to contribute as I don't remember java at all.
ModeratorThere are animal crackers for people and there are people crackers for animals.
Abydos1
Profile Blog Joined October 2008
United States832 Posts
May 28 2009 00:58 GMT
#11
http://java.sun.com/docs/books/tutorial/essential/exceptions/
"...perhaps the greatest joy possible in Starcraft, being accused of being a maphacker" - Day[9]
Malongo
Profile Blog Joined November 2005
Chile3472 Posts
May 28 2009 01:01 GMT
#12
HOE I have a suggestion to make life easier to you:
Identation: helps to find easily where your code blocks belong, I hate to see a code that ends in 3/4 brackets and I dont know where the fuck they belong.

Help me! im still improving my English. An eye for an eye makes the whole world blind. M. G.
HeavOnEarth
Profile Blog Joined March 2008
United States7087 Posts
May 28 2009 01:02 GMT
#13
i'll post in a sec (at the bullshit)
"come korea next time... FXO house... 10 korean, 10 korean"
Abydos1
Profile Blog Joined October 2008
United States832 Posts
May 28 2009 01:02 GMT
#14
On May 28 2009 10:01 Malongo wrote:
HOE I have a suggestion to make life easier to you:
Identation: helps to find easily where your code blocks belong, I hate to see a code that ends in 3/4 brackets and I dont know where the fuck they belong.



fyi, it is indented but quote doesn't show them correctly.
"...perhaps the greatest joy possible in Starcraft, being accused of being a maphacker" - Day[9]
Malongo
Profile Blog Joined November 2005
Chile3472 Posts
May 28 2009 01:06 GMT
#15
On May 28 2009 10:02 Abydos1 wrote:
Show nested quote +
On May 28 2009 10:01 Malongo wrote:
HOE I have a suggestion to make life easier to you:
Identation: helps to find easily where your code blocks belong, I hate to see a code that ends in 3/4 brackets and I dont know where the fuck they belong.



fyi, it is indented but quote doesn't show them correctly.

Lollol true.
Help me! im still improving my English. An eye for an eye makes the whole world blind. M. G.
HeavOnEarth
Profile Blog Joined March 2008
United States7087 Posts
May 28 2009 01:12 GMT
#16
On May 28 2009 09:53 Abydos1 wrote:
Show nested quote +
On May 28 2009 09:28 HeavOnEarth wrote:

import java.util.Scanner;

public class UserInput{

public UserInput(){

try{
String done = "DONE";
System.out.println("Type a string. Max 20 characters.");
System.out.println("Type DONE, in all caps to end.");
Scanner scan = new Scanner(System.in);
String s = scan.next();
while(s.compareTo(done) != 0){
if(s.length() > 20){
/**
* Here is the problem, it is ending the process,
* when it is obviously catching the exception.
*/
throw new StringTooLongException();
}
System.out.println(s);
s = scan.next();
}
}
catch(StringTooLongException e){
System.out.println(e.getMessage());
System.out.println("Exception caught! Ending program.");
}
}
}



If you're talking about this one the exception keeps going up the stack until it gets caught or the program ends, thus its getting out of the while loop.


Thank you for actually knowing what you're talking about. I will look into this, after I rape some BM'ing assholes below.
-------------------
On May 28 2009 09:54 RaGe wrote:
Maybe you should stop being so defensive and read the fucking solution for a change.

Show nested quote +
On May 28 2009 09:38 RaGe wrote:
When an exception gets thrown, it immeadiately skips to the Catch, which is at the end of your program, so it terminates the program cause there's no following lines of code



Your catch block is outside of the while loop, so it doesn't continue in the while loop after it executes the catch block. Java does not magically read your mind. This wasn't supposed to work. This isn't some obscure error. This is you skipping through whatever 'How to handle exceptions' guide you read, copy pasting some stupid shit that you don't understand, and asking tl to fix your errors.


Maybe you should learn Java before you post. You can't put catch statement inside a try statement. Idiot.

On May 28 2009 09:55 qrs wrote:
^yes, but your catch block is outside the while loop. It's the same as if you would have used goto in C.
edit: Rage pre-empted me

Same to you, cept I'm going to be nice about it. A catch can't go inside a try statement.
-------------------
On May 28 2009 09:55 Malongo wrote:
I dont understand this program does exaclty what you want imo. LOL.

On May 28 2009 09:56 Malongo wrote:
Show nested quote +
On May 28 2009 09:49 HeavOnEarth wrote:
On May 28 2009 09:38 RaGe wrote:
When an exception gets thrown, it immeadiately skips to the Catch, which is at the end of your program, so it terminates the program cause there's no following lines of code

program is doing what you made it to do lol


Firstly, the throw is inside a while loop. What I'm wondering is why the crap it isn't continuing the while loop, opposed to what it does now; which is ends the program. It should not be ending the program, it should be throwing an error, catching it, and then going back to the while loop.

On May 28 2009 09:44 RaGe wrote:
wow i get more and more convinced you have no idea what you're doing and just copy pasted shit or something

i mean, your catch block is:

catch(StringTooLongException e){
System.out.println(e.getMessage());
System.out.println("Exception caught! Ending program.");
}


you even print that you're ending the program and you dont want it to end the program?


Oh noes, I has a random print line that hasn't been changed from a previous version. Printing text doesn't end a program. ... (At least not this one)

Catch is outside the loop... -_-


It does what it fucking does, and I don't understand why it's ending instead of continuing.
And again, catch can't go inside a try.

On May 28 2009 10:01 Malongo wrote:
HOE I have a suggestion to make life easier to you:
Identation: helps to find easily where your code blocks belong, I hate to see a code that ends in 3/4 brackets and I dont know where the fuck they belong.


Die. Please.
"come korea next time... FXO house... 10 korean, 10 korean"
qrs
Profile Blog Joined December 2007
United States3637 Posts
Last Edited: 2009-05-28 01:17:54
May 28 2009 01:17 GMT
#17
Dude, no one is saying that the catch should go inside the try. If you want the while loop to continue, then the catch should go inside the while loop. Why don't you read what people write? Try is not a loop.
'As per the American Heart Association, the beat of the Bee Gees song "Stayin' Alive" provides an ideal rhythm in terms of beats per minute to use for hands-only CPR. One can also hum Queen's "Another One Bites The Dust".' —Wikipedia
aers *
Profile Joined January 2009
United States1210 Posts
May 28 2009 01:17 GMT
#18
Put the try inside the while loop.
Chill
Profile Blog Joined January 2005
Calgary25981 Posts
May 28 2009 01:19 GMT
#19
Stop being so aggressive to the suggestions here. Also please stop martyring yourself.

People are giving you good advice and your reaction to it is terrible.
Moderator
RaGe
Profile Blog Joined July 2004
Belgium9947 Posts
May 28 2009 01:20 GMT
#20
First of all: Tell me where I tell you to put your catch block inside your try block? Right, no where. You're the one that doesn't know where to put it, not me.

Secondly: Don't tell me to go learn some Java when you're learning the basic shit yourself and don't even realize how retarded all your previous statements were.

Lastly: Can you please tell me who the fuck you're sharing your account with that is making these dumbass statements? Apparently your IP has been static for days, and now it changes for every post you make about this problem.

Abusing your forum regular status to get answers for a friend that's apparently retarded. Way to fucking go.
Moderatorsometimes I get intimidated by the size of my right testicle
1 2 3 4 5 Next All
Please log in or register to reply.
Live Events Refresh
OSC
00:00
Mid Season Playoffs #2
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Nina 163
Livibee 80
ROOTCatZ 65
StarCraft: Brood War
soO 65
Noble 59
JulyZerg 27
ajuk12(nOOB) 25
sSak 17
Bale 12
Icarus 8
Dota 2
monkeys_forever544
NeuroSwarm129
League of Legends
JimRising 620
Counter-Strike
Stewie2K766
semphis_24
Other Games
summit1g6113
shahzam796
C9.Mang0302
Maynarde124
SortOf98
Trikslyr46
Organizations
Other Games
gamesdonequick867
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• Diggity2
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Rush1361
• Lourlo925
• Stunt313
Upcoming Events
Sparkling Tuna Cup
5h 28m
Afreeca Starleague
5h 28m
Light vs Speed
Larva vs Soma
2v2
6h 28m
OSC
8h 28m
PiGosaur Monday
19h 28m
LiuLi Cup
1d 6h
RSL Revival
2 days
Maru vs Reynor
Cure vs TriGGeR
The PondCast
2 days
RSL Revival
3 days
Zoun vs Classic
Korean StarCraft League
3 days
[ Show More ]
BSL Open LAN 2025 - War…
4 days
RSL Revival
4 days
BSL Open LAN 2025 - War…
5 days
RSL Revival
5 days
Online Event
5 days
Wardi Open
6 days
Liquipedia Results

Completed

Proleague 2025-09-10
Chzzk MurlocKing SC1 vs SC2 Cup #2
HCC Europe

Ongoing

BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
LASL Season 20
RSL Revival: Season 2
Maestros of the Game
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1

Upcoming

2025 Chongqing Offline CUP
BSL World Championship of Poland 2025
IPSL Winter 2025-26
BSL Season 21
SC4ALL: Brood War
BSL 21 Team A
Stellar Fest
SC4ALL: StarCraft II
EC S1
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.