|
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! :/
|
Belgium9944 Posts
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
|
Belgium9944 Posts
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?
|
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)
|
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.
|
Belgium9944 Posts
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.
|
^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
|
I dont understand this program does exaclty what you want imo. LOL.
|
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... -_-
|
United States24579 Posts
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.
|
|
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.
|
i'll post in a sec (at the bullshit)
|
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.
|
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.
|
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.
|
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.
|
aers
United States1210 Posts
Put the try inside the while loop.
|
Calgary25963 Posts
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.
|
Belgium9944 Posts
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.
|
|
|
|