The Artima Developer Community
Sponsored Link

Java Answers Forum
JOptionPane and Exception question

6 replies on 1 page. Most recent reply: Oct 27, 2005 7:31 AM by Tim W

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

JOptionPane and Exception question Posted: Oct 27, 2005 2:12 AM
Reply to this message Reply
Advertisement
Hi all,

Just have a couple of queries that I would some clarification on.

Suppose I decide not to handle a particular exception inside a method, what syntactical changes must be made to the method handling? And 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?

Ive got this program below 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:



import javax.swing.*;
import javax.sound.midi.*;

public class MidiDeviceOpener {

public static void main (String args[]) {
MidiDevice.Info [] deviceList=MidiSystem.getMidiDeviceInfo ();

MidiDevice.Info deviceInfo =
(MidiDevice.Info)
JOptionPane.showInputDialog (
null,
"Select MIDI Device",
"Program Name",
JOptionPane.QUESTION_MESSAGE,
null,
deviceList,
null);

System.out.println ("Select Device = " + deviceInfo);
if (deviceInfo !=null) {
try {
MidiDevice device = MidiSystem.getMidiDevice (deviceInfo);
device.open();
// Actions to operate the device go here
device.close ();
}
catch (MidiUnavailableException e) {
System.out.println ("Device not available");
}
}
}
}



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?

Thanks for your time.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: JOptionPane and Exception question Posted: Oct 27, 2005 4:18 AM
Reply to this message Reply
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.

Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: JOptionPane and Exception question Posted: Oct 27, 2005 5:06 AM
Reply to this message Reply
Thankyou for your explanations.

Back to my MidiDeviceOpener program which lists all MIDI devices connected to a system and asks the user to select one of them, and then will open the device if its available:

import javax.swing.*;
import javax.sound.midi.*;
 
public class MidiDeviceOpener {
 
	public static void main (String args[]) {
			MidiDevice.Info [] deviceList=MidiSystem.getMidiDeviceInfo ();
			
			MidiDevice.Info deviceInfo =
				(MidiDevice.Info)
				JOptionPane.showInputDialog (
			null,
				"Select MIDI Device",
				"Program Name",
		              JOptionPane.QUESTION_MESSAGE,
				null,
			        deviceList,
				 null);
			
			System.out.println ("Select Device = " + deviceInfo);
			if (deviceInfo !=null) {
				try {
					MidiDevice device = MidiSystem.getMidiDevice (deviceInfo);
					device.open();
					// Actions to operate the device go here
					device.close ();
				}
				catch (MidiUnavailableException e) {
					System.out.println ("Device not available");
				}
			}
    }
}
 


I would like to modify the program so its throws a MidiUnavailableException and change the println () call so that it produces a more useful error message.


What would the best way be to do this?

Thanks

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: JOptionPane and Exception question Posted: Oct 27, 2005 5:43 AM
Reply to this message Reply
As I allready mentioned:
You just have to remove the exception handling (the try ... catch part) and add "throws MidiUnavailableException" to the method's header.
But: This way you can't println anything.

That's why you need to keep the exception handling, in the "catch" section you print out whatever you want then throw the exception again.

Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: JOptionPane and Exception question Posted: Oct 27, 2005 6:19 AM
Reply to this message Reply
Could you please show me in the code where i would put it im unsure.

wouldnt it be something like

public static MidiDevice getMidiDevice(MidiDevice.Info info)
              throws MidiUnavailableException
}
catch (MidiUnavailableException e) {
	System.out.println ("Device not available");
 


Or am i completely wrong?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: JOptionPane and Exception question Posted: Oct 27, 2005 7:26 AM
Reply to this message Reply
I'm still not sure what exactly do you try to do.
As I understood you want to print some information about the exception and forward the exception to the caller of your method or something. What I don't understand is why you put everything in the main method since there is no caller who could do something useful with the exception.


The "throws ..." goes to the method header, not to the method call.

In this case:
    public static void main (String[] args) throws MidiUnavailableException {
        .
        .
        .
        if (deviceInfo !=null) {
            try {
                MidiDevice device = MidiSystem.getMidiDevice (deviceInfo);
                device.open();
                // Actions to operate the device go here
                device.close ();
            } catch (MidiUnavailableException e) {
                System.out.println ("print out somethigng useful like " e.getMessage());
                throw e; //don't know if this actually works or if you have to create a new exception.
            }
        }
    }

Tim W

Posts: 12
Nickname: daniel1980
Registered: Oct, 2005

Re: JOptionPane and Exception question Posted: Oct 27, 2005 7:31 AM
Reply to this message Reply
Thanks for that, that seems to work.

Thanks for your help

Flat View: This topic has 6 replies on 1 page
Topic: Query on tracking the selected checkbox values across the paging Previous Topic   Next Topic Topic: Help!! Can I send command to Modem?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use