Thread: Exception handling

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    21

    Question Exception handling

    So I'm writing an interface program for a particular device and there seems to be some strange goings ons. I'm rather new to this so I'm not quite sure what to do, heres the problem:

    Basically, the people who wrote the driver software for the device apparently did it in Java. However, they offer a development kit with the ability to program in C/C++ and I'm guessing the environment is able to convert from C++ to Java...?

    So anyways, during runtime there are exceptions, as will always be the case during debugging; however, since the drivers are Java I get Java error messages. Try/Catch will not work here because theyre not C/C++ exceptions.

    All I want to do is hide the Java errors and print less cryptic error messages to the screen. I could probably think of something if the exceptions were generated from boolean functions, but they're not. There must be some way around this, any ideas? (Switching to Java is not an option.)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Huh? Which OS is this? I don't know of a single OS that supports Java as driver-language? Or is this one of those drivers that isn't "a real driver", but rather there's a trivial low-level driver [which essentially is just exposing some basic interface to the hardware to the upper layer], and the "driver" you are talking to is a user-mode application that interfaces to the low-level driver?

    Any chance you could tell us what the device is? There may be a better chance of getting something useful if we can for example look up the driver?

    --
    Mats

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    21
    Ok. I think the second option you give is the most feasible. Its a spectrometer so I'm guessing the java software does interface with the low level functionality of it.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quite possibly. Unfortunately, I'm not able to help you further, I was just trying to understand more (to see if I could help or not).

    Perhaps someone with experience in Java-programming is able to help?

    Some ideas for "self-help": What is causing those exceptions? I do understand that exceptions aren't always "failures", but I donn't think that exceptions should be part of the "common path", so perhaps you can prevent the exception in the first place? Do you have source code / documentation to explain what the exceptions are and how to avoid them?

    --
    Mats

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    21

    Question

    I'd like to avaoid the exceptions and of course, in most cases, I will. However there is one particular part, in the very beginning of the program that needs to iterate through particular options until the correct one is found. Basically it searches through the com ports until the correct one, ie, the one the spectrometer is attached to, is found.

    looks like this:

    Code:
                     for(int i = 0; i < 10; i++)
    	{
    	    cout << "Trying index " << i << "..." << endl;
    	    usb2000.openSpectrometer(i);
    	}
    would love for it to look like this:


    Code:
                     for(int i = 0; i < 10; i++)
    	{
                        try
                       {
    	    usb2000.openSpectrometer(i);
                       }
                        catch(...)
                       {}
    	}
                    cout << "Spectrometer initialized..." << endl;
    if it was a boolean function it would be easy, but alas doesnt return a value.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Well, if the error messages are getting piped out beyond the control of the calling function and the exception handling mechanism, then I'd say C++ can't help you here. But then I've never done a mixed-language project.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so here's a "out-of-the-box" idea: If it's only the searching for which port you can't fix in your C++ code, how about writing a samll Java-function that does just that, using exception handling to find the right port, and then pass the port number back to your C++ program.

    --
    Mats

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    21
    Is that something you know how to do? If so, could you explain it to me? For instance passing an integer from a Java application to my c++ application? I can't find specific information on how to do such a thing.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Mr.Sellars View Post
    Is that something you know how to do? If so, could you explain it to me? For instance passing an integer from a Java application to my c++ application? I can't find specific information on how to do such a thing.
    Unfortunately, no. One way would of course be to start another application, let it write the result to a known file [1], and when the app finished, get the data out of the file (unless the app "failed" - e.g. there wasn't ANY of your hardware...

    [1] If you don't want to use a file for some reason, another alternative would be to create a pipe and connect one end of that to stdout of the application, and read the other end in your app - but I would use the file-approach if you don't have a strong reason NOT to do so.

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    21
    I guess we think the same, I was hoping I didn't have to do that but it looks like the only alternative at this point. Do you know how I might call the small Java program from within the larger C++ application? Writing to a file is fine, but I'm not sure how to actually start the other program to initialize the value written to the file. Two separate executables isnt very clean.

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Could you run a new thread calling a function from a compilation unit written in another language? Mouthful.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Mr.Sellars View Post
    I guess we think the same, I was hoping I didn't have to do that but it looks like the only alternative at this point. Do you know how I might call the small Java program from within the larger C++ application? Writing to a file is fine, but I'm not sure how to actually start the other program to initialize the value written to the file. Two separate executables isnt very clean.
    I'm pretty sure it would have to be two separte executables. Unfortunately.

    --
    Mats

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    usb2000.openSpectrometer(i);
    This should return an error if the function failed. If it does not and this is a driver specific function then I'd say the driver creators didn't do their job.

    Because then it would be:

    Code:
    if (usb2000.openSpectrometer(i))
    {
      //Success
    } else //Failure

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    21
    Its a small company that made the spectrometer and I can tell you that NO, they didn't do their job correctly. The "usb2000.openSpectrometer(i)" is an expression of type void. WHY??? I couldn't begin to speculate. And this seems to be just the beginning of myriad issues regarding their development process but I won't bore anyone with that. For now, until I can figure out how to call a Java program, during runtime, from my C++ application, it looks like I'll have to write to a file using two separate executables. Thanks for trying.

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Does openSpectrometer throw an exception? Since the function does not return anything and yet the job of the function is to attempt to open the device if it does not throw an exception there is pretty much no way to catch an error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  4. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 08:54 AM
  5. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM