How do you handle errors when calling another class initializer?

Apachi

I am trying to do some error handling while also initializing new classes. I'm surprised at the way it is set up and am hoping that I'm just missing something very simple. Here's a simple version of what I'm trying to accomplish:

Public Class TestClass
    Public Sub New(ByVal sLink as String)
        Try
            Me.New(New Uri(sLink))
        Catch ex As Exception
            MessageBox.Show("Hey, initializing this class failed...  Is the URL valid?")
        End Try
    End Sub
    Public Sub New(ByVal uLink as Uri)
        MessageBox.Show("Class Initialized Successfully!")
    End Sub
End Class

The above obviously fails because the line with "Me.New(...)" must be the first line. I could do that however, what if somebody passes a string that is not a valid Uri? Consider the following:

' This would fail and I'd like to catch it somehow
Dim tmp as New TestClass("Hello World!")
' Something like this would pass successfully
Dim tmp as New TestClass("http://somevalidlink.com/")
' And so would this, too.
Dim tmp as New TestClass(New Uri("http://somevalidlink.com/")

I have been searching and can't seem to find anything... Maybe I just don't know the keywords to look for. Any hints in the right direction would be a great help.

Thanks!

Fabio

I think you don't need to catch errors your class not responsible for.
Instead check input parameter and throw exception if parameter is wrong.
By throwing exception you will sure that class will work properly.

Public Class TestClass

    Private _Link As Uri

    Public Sub New(ByVal link as Uri)
        If link Is Nothing Then Throw New ArgumentNullException(NameOf(link))
        _Link = link
    End Sub

End Class

If you want add feature to create instance by String parameter and still using constructor with parameter of type Uri, then you can create a static/shared method which check string and execute constructor you want.

Public Shared Function CreateInstance(link As String) As TestClass
    Try
        Dim url As New Uri(link)
        Return New TestClass(url)
    Catch ex As Exception
        'Do your error handling
    End Try
End Function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you handle rename operations when calling IContextMenu?

How to handle errors that occur inside an initializer macro?

Koa.js and streaming. How do you handle errors?

How do I handle errors that are already handled by another error?

How do I handle errors when responseType is blob using Vuejs?

How do I handle different errors when working with an API?

How do you make a utility class handle differences in subclasses?

What exaclty happens when a you call a constructor(new Class),do instance initializer blocks runs first?

How do you call a method in one class, from another class?

How do you access the member functions of a class within another class?

How do you have a class change the value of an attribute of another class?

how do you handle a double image file when it`s edited

How do you sequentially handle trying multiple lines of code that might throw errors?

How do you render a list of classes inside another class in ReactJS?

How do you use parameter specifications for a class from another application?

How Do You Pass a Variable from One Class to Another in Java?

how do you call a method from another react class component

How to handle database delay on node-mysql when calling method from another js script

what event is fired when you close a javafx application from the task bar and how do you handle it?

How do you complete a CompletableFuture when another set of CompletableFutures is complete?

No visible @interface for 'AppDelegate' when calling another class

NullPointerException when calling method from another class

How do I handle Import Errors?

How do I handle errors in a deferred function?

How do I handle errors with promises?

How do I handle JavaScript Fetch errors?

How do I handle RapidXml errors?

How do I handle transaction errors?

How Do I Handle Errors Globally in TestCafe