MailBee.NET Objects 4.0

MailMessage.ThrowExceptions Property

Gets or sets whether the MailMessage object will throw exceptions on errors.

public bool ThrowExceptions {get; set;}

Property Value

true if MailMessage object will throw exceptions on errors; otherwise, false. The default value is true.

Remarks

If this property is set to true and an error occurs, an exception derived from MailBeeException is thrown.

Note    The code of the last occurred error is always contained in LastResult property. The list of all error codes is available in ErrorCodes class overview.

Even if this property is set to false, the exceptions which occur due to errors in MailBee usage will still be thrown. Typical example of such errors is passing invalid arguments to methods.

Example

This sample simulates an error and exception by attempting to load a file with non-existent name. The first attempt is made with exceptions turned off, the second - with exceptions turned on.

[C#]
using System;
using MailBee;
using MailBee.Mime;

class Sample
{
    static void Main(string[] args)
    {
        MailMessage msg = new MailMessage();

        // Turn off exception handling.
        msg.ThrowExceptions = false;

        // Generate an error.
        if (!msg.LoadMessage("Wrong Path"))
        {
            Console.WriteLine("Error occurred. Error code is " + msg.LastResult.ToString());
        }

        // Turn on exception handling.
        msg.ThrowExceptions = true;

        // The code below will throw an exception.
        try
        {
            msg.LoadMessage("Wrong Path");
        }
        catch (MailBeeException ex)
        {
            // But we catch it. We display the type of the exception. In your app,
            // you can check other useful properties such as Message or ErrorCode.
            Console.WriteLine("\r\nThe following exception was thrown:\r\n" + ex.GetType().ToString());
        }
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Mime

Module Sample
    Sub Main(ByVal args As String())
        Dim msg As New MailMessage

        ' Turn off exception handling.
        msg.ThrowExceptions = False

        ' Generate an error.
        If Not msg.LoadMessage("Wrong Path") Then
            Console.WriteLine("Error occurred. Error code is " & msg.LastResult.ToString())
        End If

        ' Turn on exception handling.
        msg.ThrowExceptions = True

        ' Throw an exception.
        Try
            msg.LoadMessage("Wrong Path")
        Catch ex As MailBeeException
            ' But we catch it. We display the type of the exception. In your app,
            ' you can check other useful properties such as Message or ErrorCode.
            Console.WriteLine(vbCrLf & "The following exception was thrown:" & vbCrLf & _
                ex.GetType().ToString())
        End Try
    End Sub
End Module

See Also

MailMessage Class | MailBee.Mime Namespace | MailBeeException | LastResult