ex-ex-01-math-interpreter/MathInterpreter/Core/Exceptions.cs
2025-06-05 14:36:50 +02:00

60 lines
No EOL
2.5 KiB
C#

using System;
namespace MathInterpreter.Core;
public abstract class ExpressionException : Exception
{
/// <summary>
/// Initializes <see cref="ExpressionException"/> with the specified error <paramref name="message"/> and the optional <paramref name="innerException"/>
/// </summary>
/// <param name="message">The error message</param>
/// <param name="innerException">The optional exception</param>
protected ExpressionException(string message, Exception? innerException = null)
: base(message, innerException) { }
}
public sealed class ExpressionFormatException : ExpressionException
{
/// <summary>
/// Initializes <see cref="ExpressionFormatException"/> with the specified error <paramref name="message" /> and the optional <paramref name="innerException"/>
/// </summary>
/// <param name="message">The error message</param>
/// <param name="innerException">The innerException</param>
public ExpressionFormatException(string message, Exception? innerException = null)
: base(message, innerException) { }
}
public sealed class OperatorException : ExpressionException
{
/// <summary>
/// Initializes <see cref="OperatorException"/> with the specified error <paramref name="message"/>
/// </summary>
/// <param name="message">The error message</param>
public OperatorException(string message) : base(message) { }
}
public sealed class NumberFormatException : ExpressionException
{
/// <summary>
/// Initializes <see cref="NumberFormatException"/> with the <paramref name="expression"/> and the optional <paramref name="reason"/>
/// </summary>
/// <param name="expression">The expression</param>
/// <param name="reason">The optional reason</param>
public NumberFormatException(ReadOnlySpan<char> expression, string? reason = null)
: base(FormatMessage(expression, reason)) { }
private static string FormatMessage(ReadOnlySpan<char> expression, string? reason = null)
{
reason = string.IsNullOrWhiteSpace(reason) ? string.Empty : $" ({reason})";
return $"Unable to read number from beginning of '{expression}'{reason}";
}
}
public sealed class NumberValueException : ExpressionException
{
/// <summary>
/// Initializes <see cref="NumberValueException"/> with the specified error <paramref name="message"/>
/// </summary>
/// <param name="message">The error message</param>
public NumberValueException(string message) : base(message) { }
}