Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6624735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:41:22+00:00 2026-05-25T21:41:22+00:00

I know that exception driven programming is something that shouldn’t be done (and I

  • 0

I know that exception driven programming is something that shouldn’t be done (and I imagine why).
I’m building a library where I have to estabilish a connection with a device, so the programmer will call MyDeviceInstance.Connect on some point and now is where my problem shows up.

I wrote my connect methods as void Connect(), so I’m not returning anything (because actually I don’t need anything), but I will throw exception in a lot of situations:

  1. If connection already exists
  2. If there are some wrong settings in MyDeviceInstance
  3. If my interop method deviceConnect will fail (and here is the biggest problem, because it will returns an int that represents a lot of errors)

Should Connect returns a boolean instead of void?There will be a lot of possibilities that I’ll throw an exception.

The second part of this question is on another method, called void Update(). This method can works only if MyDeviceInstance is already connected. In this case I’ll throw exceptions when:

  1. Connection doesn’t exist
  2. Connection is lost for any reason (and the programmer will not expect this will happen, but can)
  3. Other interop errors

Should update returns a bool value instead of throwing all those exceptions (at least, to limit common errors like “connection doesn’t exist”)?

Thanks for any answer

EDIT 1:

I have to be more precise on one point: because we are talking about an USB device, when I’ll call my method Update, this method could fail depending on the device (if is connected or not). More important, if I reconnect the device the interop method will start working again (and the normal method will do the same), without needing a reconnection or things like that (I don’t know how this is working internally).

For update methods, should I throw if the device is disconnected? Programmatically speaking this is an exception, but user-speaking this will be a quite-normal situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:41:23+00:00Added an answer on May 25, 2026 at 9:41 pm

    If the code which calls a method will be prepared to deal with unusual results, the method should indicate unusual results via some means other than exceptions. If the calling code will not be prepared to deal with unusual results, then such results should cause exceptions. Unfortunately, there’s no way for a class to magically know whether the caller is going to expect unusual results unless the caller tells it somehow. A common pattern in .net is for a class to provide pairs of “DoSomething” and “TryDoSomething” methods:

    ResultType DoSomething();
    Boolean TryDoSomething(ref ResultType result);
    

    Frankly, I think the latter formulation would be better expressed as:

    ResultType TryDoSomething(ref Boolean ok);
    

    or perhaps:

    ResultType TryDoSomething(ref Exception ex);
    

    where “ex”, if non-null, would indicate that an exception (ex) would have been thrown by DoSomething (but with a blank stack trace, since the exception won’t actually have been thrown). Another pattern in some cases may be:

    ResultType TryDoSomething(Action failureAction);
    

    If an unexpected situation arises, the code will call the indicated delegate, with a parameter providing details of the context where the situation arose. That delegate may be able to allow for better error handling and recovery than would otherwise be possible, since it could be called before TryDoSomething gave up on whatever it was doing. For example, it could direct TryDoSomething() to keep retrying the operation unless or until the object supplying the delegate receives a cancel request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
We know that no matter whether an exception is thrown, or caught and handled
We know that we can catch any unexpected exception at application level by using
I know from wikipedia for example that exception handling is used in an application
I know that exceptions have a performance penalty, and that it's generally more efficient
We all know the effects that lots of thrown exceptions can have over the
I know that I can do something like $int = (int)99; //(int) has a
We all know that SQLException is a checked Exception and most of us agree
I'm starting out with DBus and event driven programming in general. The service that
I know that Java enforce the programmer to list all exceptions that will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.