I’d like some advice on building in known errors. Let’s say I have a Windows form that needs to set the source path of an image in an object. It must be:
- A valid path
- An image
- A PNG
- 32 x 32 in size
- No transparency
The thing about catching errors is that I’d like the class to handle the errors as much as possible, instead of the Windows form.
So let’s say I had:
Public Class MyImage
Public Property SourcePath As String
End Class
and
Sub TestImage()
Dim imgPath As New MyImage
Try
imgPath.SourcePath = "C:\My Documents\image001.png".
Catch ex As Exception
MsgBox(ex)
End Try
End Sub
SourcePath should be a string path that points to a valid image file, that is a png, that is 32×32 and has no transparency. If it is not one or more of those, I just want the ex to report back what error(s) are there (like “The image is not 32×32” or “The image contains transparency, which is should not. And it also is not 32×32.). How can I create my own Exceptions for the property SourcePath above?
On top of this, let’s say I had all the same requirements above, but instead of a 32×32 size, I required a 48×48 size for the image on the SourcePath. Is there a way to customize for this?
Thx in advance
use something like this:
You should probably not be throwing exceptions while setting the SourcePath property. Possibly either have that logic in the constructor (accept a string, sourcepath into the constructor and throw validations). Either way, the code would look something like this…
Then your code would look like this…