i personally am having a hard time grasping the exceptions thing and honestly the JAVA language as a whole. I am finding it very rough to focus on and am slowly falling behind in my class. do you know of any good tutorials or books that may help me graspe it faster or easier? i have another assignment due this week and I have no clue (unfortunately) on how to even start
throw an exception in code
Collapse
X
-
-
Originally posted by Dlangdamani personally am having a hard time grasping the exceptions thing and honestly the JAVA language as a whole. I am finding it very rough to focus on and am slowly falling behind in my class. do you know of any good tutorials or books that may help me graspe it faster or easier? i have another assignment due this week and I have no clue (unfortunately) on how to even startComment
-
Originally posted by Dlangdamani personally am having a hard time grasping the exceptions thing and honestly the JAVA language as a whole. I am finding it very rough to focus on and am slowly falling behind in my class. do you know of any good tutorials or books that may help me graspe it faster or easier? i have another assignment due this week and I have no clue (unfortunately) on how to even start
You may not know the answer so let me try and break it down into a few topics at least. Help us to help you. ;)
-What is an exception?
-What is happening in the exception "class" you were suggested to create for yourself? (i.e. what does super(s) mean? If you've never done java, you may not know.)
-What does "throw new MyException(Str ing message)" actually do?
I honestly think you're tackling too much at a time right now from the way you sound. Perhaps you're trying to tackle a problem that takes too much previous knowledge. Take it slow. Find out what every single thing means, it's a lot to take in, but with a good amount of hard work I'm sure you can do it.
I'll continue with some more questions, there are more...
-What is does putting part of my code in try brackets do? What does putting part of my code in catch brackets do?
-How does this all relate? Why throw an exception? Why catch it?
I'll try to explain a few things here, hopefully it'll help. Most of this is repeated summarization:
In part of your code you want to throw an exception when the grade is above 100 or below 0 right? So somewhere you throw the exception like this:
Code:throw new MyException("message")
Code:class MyException extends Exception //extending means you basically inherit the methods of that class. Exception is the master class of all exceptions and all other exceptions must extend the Exception class or a class that already extends it... { public MyException(String s) { super(s); //super(s) means that in this circumstance you do to the string whatever your "extended" class would do to it. In this case super(s) for the exception class simply saves the message. So when you throw new MyException("an exception occured") that will be sent with the exception, wherever it is caught (catch...?) } }
Somewhere else, wherever you run this method, you have to catch the exception. What does that mean? Well, like we said earlier, a thrown exception just tells the computer that an exception was thrown. Wherever you catch it, that's where you tell the computer what to do if you catch an exception. Anything making better sense?
Also, I suggest keeping it simple. Don't add a dialog pop up. Just use the scanner class and ask the user a question and request an input. Have you learned to do that yet?
Anyways, good luck again, and don't give up!
-blazedComment
-
-
Originally posted by DlangdamanI have not yet learned that
recommend that option) or start reading up on what Exceptions are all about.
It's not that difficult; you'll get the hang of it.
Basically you throw an Exception when normal control flow can't continue and
you don't know what to do anymore to 'repair' the situation. It's up to the caller
of your method to 'handle' that Exception, i.e. your method throws an Exception,
a caller catches it and handles it; if it can't handle it, is shouldn't catch it and
leave the handling to its own caller, etc. etc. that's all there is to it.
kind regards,
JosComment
-
it unfortunately is needed..we have touched them, what I meant we havent learned is how to use the scanner classComment
-
Originally posted by Dlangdamanit unfortunately is needed..we have touched them, what I meant we havent learned is how to use the scanner class
The scanner class is just an easy way to read inputs.
For example:
[code=java]
import java.util.*; //The scanner class is inside java.util.Scann er
public class ScannerExample {
public static void main(String args[]) {
Scanner scannerVariable Name = new Scanner(System. in); //The way to declare scanner
System.out.prin tln("Please input a string: "); //Asks the user to input a string
String inputtedString = scannerVariable Name.next(); //Accepts a string input
System.out.prin tln();
System.out.prin tln("The following is the inputted string tokenized by whitespace: " + inputtedString) ;
//Now if you wanted to input an int you could do this:
System.out.prin tln("Please input an int: "); //Asks the user to input an int
int inputtedint = scannerVariable Name.nextInt(); //Accepts an int input
System.out.prin tln();
System.out.prin tln("The following is the inputted int: " + inputtedint);
}
}
[/code]
The output after you compile would look like this:
Code:--------------------Configuration: <Default>-------------------- Please input a string: examplestring The following is the inputted string tokenized by whitespace: examplestring Please input an int: 10 The following is the inputted int: 10 Process completed.
Now the scanner class can do a lot more then that, to find out more, check out it's documentation: http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html
Good luck man,
-blazedComment
Comment