File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Questions - Exceptions, JOptionPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Questions - Exceptions, JOptionPane" Watch "Questions - Exceptions, JOptionPane" New topic

Questions - Exceptions, JOptionPane

Daniel Jones

Joined: Oct 26, 2005
Posts: 2
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

Joined: Jul 08, 2003
Posts: 24199


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 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.

[Jess in Action][AskingGoodQuestions]
Daniel Jones

Joined: Oct 26, 2005
Posts: 2
Ok thanks for your advice. In future i will do that
I agree. Here's the link:
subject: Questions - Exceptions, JOptionPane
It's not a secret anymore!