• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 03:01
CEST 09:01
KST 16:01
  • 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
RSL Season 1 - Final Week6[ASL19] Finals Recap: Standing Tall15HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0
Community News
Esports World Cup 2025 - Brackets Revealed14Weekly Cups (July 7-13): Classic continues to roll8Team TLMC #5 - Submission extension3Firefly given lifetime ban by ESIC following match-fixing investigation17$25,000 Streamerzone StarCraft Pro Series announced7
StarCraft 2
General
RSL Revival patreon money discussion thread Who will win EWC 2025? Server Blocker Weekly Cups (July 7-13): Classic continues to roll The GOAT ranking of GOAT rankings
Tourneys
FEL Cracov 2025 (July 27) - $8000 live event Sea Duckling Open (Global, Bronze-Diamond) RSL: Revival, a new crowdfunded tournament series $5,100+ SEL Season 2 Championship (SC: Evo) WardiTV Mondays
Strategy
How did i lose this ZvP, whats the proper response
Custom Maps
External Content
Mutation # 482 Wheel of Misfortune Mutation # 481 Fear and Lava Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome
Brood War
General
Flash Announces (and Retracts) Hiatus From ASL BGH Auto Balance -> http://bghmmr.eu/ [ASL19] Finals Recap: Standing Tall BW General Discussion Help: rep cant save
Tourneys
[Megathread] Daily Proleagues Cosmonarchy Pro Showmatches CSL Xiamen International Invitational [BSL20] Non-Korean Championship 4x BSL + 4x China
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile Nintendo Switch Thread CCLP - Command & Conquer League Project The PlayStation 5
Dota 2
Official 'what is Dota anymore' discussion
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 Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Stop Killing Games - European Citizens Initiative Summer Games Done Quick 2025!
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread [\m/] Heavy Metal Thread
Sports
2024 - 2025 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Ping To Win? Pings And Their…
TrAiDoS
momentary artworks from des…
tankgirl
from making sc maps to makin…
Husyelt
StarCraft improvement
iopq
Customize Sidebar...

Website Feedback

Closed Threads



Active: 625 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 States24673 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
Calgary25980 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
OSC Elite Rising Star #15
Jumy vs NicoractLIVE!
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Nina 270
StarCraft: Brood War
GoRush 319
Dewaltoss 116
Leta 92
zelot 46
ajuk12(nOOB) 19
Bale 13
Dota 2
ODPixel392
XcaliburYe257
League of Legends
JimRising 680
Super Smash Bros
Westballz27
Other Games
summit1g10211
SortOf56
Trikslyr35
Organizations
Other Games
gamesdonequick2420
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• Berry_CruncH156
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• lizZardDota2122
League of Legends
• Lourlo1963
• Rush1674
• Stunt465
Upcoming Events
Epic.LAN
4h 59m
Big Brain Bouts
8h 59m
sebesdes vs SpeCial
Harstem vs YoungYakov
GgMaChine vs uThermal
CranKy Ducklings
1d 2h
Epic.LAN
1d 4h
CSO Contender
1d 9h
BSL20 Non-Korean Champi…
1d 10h
Bonyth vs Sziky
Dewalt vs Hawk
Hawk vs QiaoGege
Sziky vs Dewalt
Mihu vs Bonyth
Zhanhun vs QiaoGege
QiaoGege vs Fengzi
Sparkling Tuna Cup
2 days
Online Event
2 days
BSL20 Non-Korean Champi…
2 days
Bonyth vs Zhanhun
Dewalt vs Mihu
Hawk vs Sziky
Sziky vs QiaoGege
Mihu vs Hawk
Zhanhun vs Dewalt
Fengzi vs Bonyth
Esports World Cup
4 days
ByuN vs Astrea
Lambo vs HeRoMaRinE
Clem vs TBD
Solar vs Zoun
SHIN vs Reynor
Maru vs TriGGeR
herO vs Lancer
Cure vs ShoWTimE
[ Show More ]
Esports World Cup
5 days
Esports World Cup
6 days
Liquipedia Results

Completed

JPL Season 2
RSL Revival: Season 1
Murky Cup #2

Ongoing

BSL 2v2 Season 3
Copa Latinoamericana 4
Jiahua Invitational
BSL20 Non-Korean Championship
CSL Xiamen Invitational
CSL Xiamen Invitational: ShowMatche
Championship of Russia 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25

Upcoming

2025 ACS Season 2
CSLPRO Last Chance 2025
CSLPRO Chat StarLAN 3
BSL Season 21
K-Championship
RSL Revival: Season 2
SEL Season 2 Championship
uThermal 2v2 Main Event
FEL Cracov 2025
Esports World Cup 2025
Underdog Cup #2
ESL Pro League S22
StarSeries Fall 2025
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
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.