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 8809871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:59:04+00:00 2026-06-14T02:59:04+00:00

I have a class which contains a Phone object. The phone is a virtual

  • 0

I have a class which contains a Phone object.

The phone is a virtual phone, and in the constructor to my class, I pass in the IP Address and the port of the virtual phone I wish to connect to.

Inside the constructor, I then run a method called Connect. In the Connect method I instantiate an IPEndPoint object out of the IP Address/Port combo of the phone, I then instantiate a Socket object, and run the Connect method of the Socket object, passing in my IPEndPoint as the parameter.

If the phone server hasn’t been configured properly for a particular phone (not my job) the connection is refused, and a SocketException is thrown. I am trying to catch this exception.

This is a Windows Form application. Within the scope of the Form object, but outside of the scope of any constructors/methods, I have my phone as an uninstantiated private field like so (IPhone is the interface which my phone uses):

private IPhone _phone;

I have a method called SetupPhone, I instantiate my phone object in here, and am trying to catch the exception here:

private void SetupPhone()
{
    try
    {
        _phone = new Phone(AgentDetails.IPAddress, AgentDetails.Port);
    }
    catch(SocketException ex)
    {
        Log.LogException("Error mapping phone to port", ex);
        ShowBaloonTip("An error occured starting CTI. Please select your name from the list to try again", ToolTipIcon.Error);

        ChangeUser();
        return;
    }

    //Subscribe to Phone events here
}

Here’s the constructor for the phone object:

public Phone(string ipAddress, int port, string password = "FooBar")
{
    Connect(ipAddress, port, password);
}

Here is the Connect method:

public void Connect(string ipAddress, int port, string password)
{
    _phone = new IPEndPoint(IPAddress.Parse(ipAddress), port);
    _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

    _socket.Connect(_phone);

    //Redacted
}

On a not properly configured phone, my SocketException is thrown as shown below:

Exception

Am I not right in thinking that any exceptions thrown by any methods run in an objects constructor should be safely caught by a try/catch block put around the instantiation of that object? Or is this not the case? I want to catch any errors that may occur when instantiating my object, is this not possible?

  • 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-06-14T02:59:05+00:00Added an answer on June 14, 2026 at 2:59 am

    http://www.blackwasp.co.uk/VSBreakOnException.aspx

    When running a program in debug mode, using Visual Studio’s debugger,
    and encountering an exception, the default behaviour is to pause
    execution unless the error is handled in a try / catch block. This
    behaviour can be modified for each exception type.

    Debug Mode Exceptions

    When you execute your software in debug mode, Visual Studio reacts
    differently to a thrown exception depending upon whether the exception
    is handled within a try / catch / finally block or is unhandled. When
    using the default configuration, unhandled exceptions cause the
    program to halt and the exception details to be displayed. Handled
    exceptions do not cause the program to stop. You can see this by
    executing the following code in debug mode. Although a
    DivideByZeroException is thrown when the division is attempted, the
    program continues. If you comment out the try and catch, the exception
    halts execution.

    try
    {
        int i = 0;
        int j = 1;
        Console.WriteLine(j / i);
    }
    catch
    {
        Console.WriteLine("Caught an exception");
    }
    Console.ReadLine();
    

    This behaviour is useful in most circumstances. However, sometimes you
    will want to ignore a specific type of exception, even when unhandled,
    or break on a handled exception that would normally be ignored. You
    can control this using the Exceptions dialog box, which may be viewed
    by selecting “Exceptions” from the Debug menu or by pressing
    Ctrl-Alt-E.

    The main area of the dialog box shows a list of exception types in a
    tree structure. The branches of the tree can be expanded to show
    various groups of exceptions and the individual types within each
    category. Two checkboxes are shown for each exception and group. If
    the “Thrown” checkbox is ticked, the program will break when the
    selected exception, or one of the selected group of exceptions, is
    encountered. This includes exceptions that have been handled. If the
    “User-unhandled” checkbox is ticked the program will break only if the
    exception is unhandled. To try the options, find the
    DivideByZeroException type in the tree structure. To find an exception
    quickly, click the Find button and enter part of the name of the item
    you are searching for. The first matching item will be found. If this
    is not the desired exception, click the Find Next button to cycle
    through the matches. Once you have found DivideByZeroException, check
    the appropriate “Thrown” option. If you run the sample program you
    will see that the program halts on the handled exception.

    Reset All

    There are some further options in the Exceptions dialog box that are
    worthy of note. The first of these is the Reset All button. If you
    have changed the options in the dialog box to help debug your
    application, you can reset all of the options to their original
    settings by clicking this button.

    Configuring Custom Exceptions

    If you have defined your own exception type that inherits
    functionality from the Exception class or one of its subclasses, you
    may wish to configure it using the dialog box. As it will not be a
    standard exception type, it will not appear in the list by default.
    However, you can add it by clicking the Add button and providing the
    details. For .NET exceptions you should ensure that you choose the
    “Common Language Runtime Exceptions” option from the drop-down list.
    You should then provide the fully qualified name of the exception
    class. For example, “MyNamespace.MyException”.

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

Sidebar

Related Questions

I have a parent class which contains a child object. I am using set
I have a class which contains sensitive information (Credit card info, phone numbers etc).
I have a class which contains a static collection that can be used across
I have a class which contains 5 properties. If any value is assingned to
Hi i have a class which contains different methods for checking database values. Each
I have an abstract class which contains method for setting header text. It looks
Alright so here is the question. I have a user class which contains a
I have a class library project which contains some content files configured with the
I have class like this below shown. which contains the shopping items where the
In my console application have an abstract Factory class Listener which contains code for

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.