|
Re: JOptionPane and Exception question
|
Posted: Oct 27, 2005 4:18 AM
|
|
Ok, at least you tried to format your code. Next time, don't use "code", but "java" instead.
Someone has to catch a known exception. If you don't want to handle an exception inside a method you must of course the exception to be thrown by the method. public ReturnType method throws ExceptionType
.
The calling method can handle the exception with a classical try{}catch(ExceptionType e){}
statement. Or it throws the exception to whoever is calling it the same way the other method did.
Exceptions can be really useful, because this way you can return a error without using a error value for the standard return value, so the calling method doesn't need to evaluate the result, but can just catch the error eventually. If it doesn't handle the exepction, it just forwards it to it's calling method. This can come in handy sometimes.
Abouct your cast questions: Every Object no matter what type has a toString() method, but that's quiet it. I don't know if JOPtionPane can render certain classes in a different way, but for default it can only display it's string representation. It accepts an array of Objects, so the selected object itself can be returned and not a index or something. JOptionPane is still programmed in the "classical" way. There are some classes like Vector or Arraylist wich can be set to a certain type (read Jdk1.5 documentation). Once the type is set they accept only objects of this type and cast the return value automatically. JOptionPane does not support this. It can only return the type Object. Now the compiler does not know which type the returned object has, so you have to tell him.
|
|