This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Just starting to learn java I have quite few questions.
Number 1 � Suppose I decide not to handle a particular exception inside a method, what syntactical changes must be made to the method handling? What steps must be taken inside the calling method to ensure that the exception is still correctly handled?
Don�t I have to change the method so it signals that the exception may be thrown. I think how you handle an exception relies on the scenario but you should never use an exception for handling a situation that is not an exception. Am I right?
Number 2 - Ive got this program which lists all MIDI devices connected to the system, and asks the user to select one of them, and then will open the device if its available:
I now wish to modify this program so it throws a MidiUnavailableExpeception and change the println () call so that it produces a more useful error message.
Is this how i would make it throw a MidiUnavilableException?, if so where would the best place be to put this in my program.
Number 3 - Got a couple of queries that im not sure about. Why is a JOptionPane.showInputDialog () able to accept an array of MidiDevice.Info objects as a parameter and render the information with each object as a string? And why does the result of this method have to be type-cast to MidiDevice?
Sorry about the long questions just dont fully understand. Thanks for your time.
We've got a guide to asking questions on the Ranch here. Parts of this might be helpful to you, especially this and this.
Now, you've got many questions here, and so many of them depend on previous answers that it's hard to answer them in any sensible way. For the first one, I think the best answer is for you to go read this section of Sun's Java Tutorial which is about exceptions. It should clear up any misconceptions you might have.
For the second question, you ask how to throw an exception, but the code you're showing seems to indicate that the MidiDevice.open() method already throws the exception in question, and you are catching it, which is quite the opposite. Again, I think that tutorial might clear things up in your mind.
About JOptionPane: it can take an array of objects because any Java object can be converted to a String with the toString() method; JOptionPane can display each iterm using that String. The returned object has to be cast back to the appropriate type just because the method you're calling is defined to return Object, and so the real type information is hidden from the compiler.