Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions - Exceptions, JOptionPane

 
Daniel Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

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.
 
Daniel Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks for your advice. In future i will do that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic