|
Post by Bahamut on Feb 23, 2002 1:44:57 GMT -5
So I suppose that when we throw something we still get those errors. ie. If I try to do a method call using a null reference I'll get a NullPointerException and the exception will be printed to the screen like it always does, whether I throw it or not. But that's the purpose of throwing: making the user deal with it.
I'm just checking to see if I understand throwing.
|
|
|
Post by Brutal_Chicken on Feb 23, 2002 12:45:53 GMT -5
Basically, you're saying to the user "You screwed up". So, yeah, you're correct.
|
|
|
Post by Sylph on Feb 23, 2002 17:22:06 GMT -5
how do u throw an Exception that extends RuntimeException? i didn't work, so i tried importing it, but it said it wasn't found in import. then i tried to import RuntimeException, but the compiler said it wasn't found in import either.
the Exceptions on p. 111 of the lecture notes, they're legit, rite?
EDIT: nevermind. i misspelled the exception.
|
|
|
Post by gabbie on Feb 24, 2002 23:33:49 GMT -5
why do we have to throw these exceptions, eg nullpointer, blah blah blah, if java automatically prints the message out on the screen by itself.... don't get it
|
|
|
Post by Bahamut on Feb 25, 2002 1:39:30 GMT -5
Because it makes your code look pretty.
|
|
|
Post by EurasianVixen on Feb 25, 2002 1:40:22 GMT -5
I don't understand how exceptions work. Say I was trying to access an index that did not exist in the LinkedList. Do I throw a NullPointerException then? But doesn't that just print on the screen "... java.lang.NullPointerException" anyways? I thought that when an exception is thrown, the program ignores it and skips over the method ... ??
|
|
|
Post by Sylph on Feb 25, 2002 20:27:23 GMT -5
I don't understand how exceptions work. Say I was trying to access an index that did not exist in the LinkedList. Do I throw a NullPointerException then? But doesn't that just print on the screen "... java.lang.NullPointerException" anyways? I thought that when an exception is thrown, the program ignores it and skips over the method ... ?? but if a user is using LinkedList, what does a nullpointerexception mean to them? they're accessing an index that is out of bounds, but they get a nullpointerexception... the user won't know what they did wrong if they get that exception.
|
|
|
Post by gabbie on Feb 25, 2002 23:16:56 GMT -5
so do i throw a message to the screen ? saying something like "YOU ENTERED THE WRONG NUMBER RETARD" or just throw the exception back, the exception it throws back at the screen neways...
|
|
|
Post by Bahamut on Feb 25, 2002 23:43:36 GMT -5
saying something like "YOU ENTERED THE WRONG NUMBER RETARD"
I will give you $20 if you do that in your assignment.
|
|
|
Post by gabbie on Feb 26, 2002 0:46:01 GMT -5
answer me please...
|
|
|
Post by Sylph on Feb 26, 2002 3:10:58 GMT -5
u probably just want an exception message, w/o the extra "helpful" stuff... sometimes the exception should be different from what it throws anyway, but if it's the same, i guess they just want us to do it formally or something... maybe it's good form...
|
|
|
Post by gabbie on Feb 26, 2002 10:37:29 GMT -5
THANKS A LOT ~!! =)
|
|
|
Post by gabbie on Feb 26, 2002 16:33:52 GMT -5
am i incharge of using a try/catch block to catch the exception and throw a friendier message to the user ? or should i just throw ??
|
|
|
Post by Bahamut on Feb 26, 2002 16:44:37 GMT -5
All you have to do is throw. That's it. Nothing more dealing with exceptions. At all.
|
|
|
Post by Evotamer on Feb 26, 2002 22:56:58 GMT -5
Exceptions are basically error messages that are printed to the screen when a program senses an error...
i.e. You have an array that is of size 10. But you wish to add something at the i'th position. The compiler doesn't know what i will be equal until the user enters it (assuming that i will be an input from the user). So the compiler allows this to go through and your program can execute...
But suppose you enter 13 as the i'th value... the computer doesn't know what to do... therefore it throws an exception to the user.
Generally Java's Exception message can be quite ambiguous.. therefore creating your own exceptions, like "YouSuckBigTime" may be a better exception depending on your situation... But as convention something like "InputValueTooLarge" would be more appropriate.
When catchin, you catch these exceptions... and if it detects 'foul play' / the exception, you can even prompt the user to retry their command.
|
|