The Artima Developer Community
Sponsored Link

Java Answers Forum
no return value

4 replies on 1 page. Most recent reply: May 10, 2002 1:27 PM by Matt Gerrans

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 4 replies on 1 page
Michael

Posts: 2
Nickname: mystic
Registered: May, 2002

no return value Posted: May 10, 2002 9:06 AM
Reply to this message Reply
Advertisement
public SerialPort schnittstelleOeffnen(CommPortIdentifier comid)
{
SerialPort sp;

try
{
sp = (SerialPort)comid.open("test", 100);
return (sp);
}
catch (PortInUseException e)
{
System.exit(1);
}
}

Compiler says that the method does not return a value! I think it's because there is no return in case of an Exception. Can somebody help me with that?

thx
mystic


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: no return value Posted: May 10, 2002 9:26 AM
Reply to this message Reply
you guessed it right. just put a return null at the end.

Michael

Posts: 2
Nickname: mystic
Registered: May, 2002

Re: no return value Posted: May 10, 2002 9:51 AM
Reply to this message Reply
it works, thx alot :)
i only wonder because i was quite sure i tried that out too, however thx again

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: no return value Posted: May 10, 2002 1:20 PM
Reply to this message Reply
You might want to consider using the finally clause in this case; it is executed whether an exception is thrown or not. Here is how I would do it:
   public SerialPort schnittstelleOeffnen(CommPortIdentifier comid)
   {
      SerialPort sp = null;
 
      try
      {
         sp = (SerialPort)comid.open("test", 100);
      }
      catch( PortInUseException piue )
      {
         // System.exit(1);
         // Try to handle the problem here, or show some useful
         // information to the user about how to solve the problem.
         // For example:
         System.out.println( "Error: Cannot open the serial port because " + 
                             "it is currently in use by another process." );
      }
      finally
      {
         return sp;
      }
   }


Alles Gute!

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: no return value Posted: May 10, 2002 1:27 PM
Reply to this message Reply
Additionally, rather than silently exit() from a catch clause, you may as well not catch the exception and let it bubble up to the caller. (Yes, I realize this is probably testing-in-progress code and that you are the only one who will see it, but you'll be treating yourself better if you can see more of the exception information).
   public SerialPort schnittstelleOeffnen(CommPortIdentifier comid) throws PortInUseException
   {
       return (SerialPort)comid.open("test", 100);
   }

Of course, that would kind of obviate the need for your method, since you could just call the comid.open() method in the code that is currently calling schnittstelleOeffnen().

Flat View: This topic has 4 replies on 1 page
Topic: List of Float numbers Previous Topic   Next Topic Topic: EJB

Sponsored Links



Google
  Web Artima.com   

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