Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Own Exceptions

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 32x32 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 32x32" or "The image contains transparency, which is should not. And it also is not 32x32.). 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 32x32 size, I required a 48x48 size for the image on the SourcePath. Is there a way to customize for this?

Thx in advance

like image 639
Stan Avatar asked Dec 13 '22 18:12

Stan


1 Answers

use something like this:

public class InvalidImageException : Exception
{
    public InvalidImageException() { }
    public InvalidImageException(string message)
        : base(message) { }
    public InvalidImageException(string message, Exception innerException)
        : base(message, innerException) { }
    public InvalidImageException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        : base(info, context) { }
    public InvalidImageException(string message, MyImage image)
        : base(message) 
    {
        this.Image = image;
    }
    public MyImage Image { get; set; }
}

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...

public class MyImage
{
    public MyImage(string sourcePath)
    {
        this.SourcePath = sourcePath;
        //This is where you could possibly do the tests. some examples of how you would do them are given below
        //You could move these validations into the SourcePath property, it all depends on the usage of the class
        if(this.height != 32)
            throw new InvalidImageException("Height is not valid", this);
        if(this.Width != 32)
            throw new InvalidImageException("Width is not valid",this);
        //etc etc
    }
    public string SourcePath { get; private set; }
}

Then your code would look like this...

try
{
    imgPath = new MyImage("C:\My Documents\image001.png");
}
catch(InvalidImageException invalidImage)
{
    MsgBox.Show(invalidImage.Message);
}
catch(Exception ex)
{
    //Handle true failures such as file not found, permission denied, invalid format etc
}
like image 54
TerrorAustralis Avatar answered Dec 22 '22 04:12

TerrorAustralis