0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

基本パーツ

Posted at

基本パーツ

基本パーツは他のソースコードでよく登場するので紹介しておきます

CustomError, CustomException

エラー処理です
System.Environment.StackTrace をコンストラクタで暗黙的に引き渡しています
スタックトレースによりエラーの発生したソースファイル名や行情報などが分かります
CustomException で例外をエラーと同様に扱えるようにしています

CustomError.cs
namespace System
{
    public class CustomError
    {
        public readonly string reason;
        public readonly string? stack_trace;

        protected CustomError(string reason, string? stack_trace)
        {
            this.reason = reason;
            this.stack_trace = stack_trace;
        }

        protected static string Trim(string s)
        {
            if (s.Length == 0)
            {
                return s;
            }

            //最初の2行を削除する

            int index = 0;

            for (int i = 0; i < 2; i++)
            {
                index = s.IndexOf('\n', index);

                if (index < 0 || ++index >= s.Length)
                {
                    return s;
                }
            }

            return s.Substring(index);
        }

        public CustomError(string reason) : this(reason, Trim(Environment.StackTrace))
        {
        }

        public CustomError(string format, params object[] a) : this(string.Format(format, a), Trim(Environment.StackTrace))
        {
        }

        protected const string crlf = "\r\n";

        public override string ToString()
        {
            string s = reason;

            if (!string.IsNullOrEmpty(stack_trace))
            {
                s += crlf + stack_trace;
            }

            return s;
        }

        public const string format0_unexpected_error = "予期しないエラーが発生しました";

        public const string format1_null_or_length_0 = "{0} が null か長さが 0 です";
        public const string format1_length_0 = "{0} は長さが 0 です";
        public const string format1_not_exist_directory = "ディレクトリが存在しません ({0})";
        public const string format1_not_exist_file = "ファイルが存在しません ({0})";

        public const string format2_out_of_range = "{0} ({1}) の値が許容範囲外です";
        public const string format2_path_is_not_exist = "{0} (\"{1}\") は存在しません";
    }

    public class CustomException : CustomError
    {
        public readonly string exception_type;

        public CustomException(Exception ex) : base(ex.Message, ex.StackTrace)
        {
            exception_type = ex.GetType().ToString();
        }

        public override string ToString()
        {
            string s = "例外が発生しました : " + exception_type + crlf + reason;

            if (!string.IsNullOrEmpty(stack_trace))
            {
                s += crlf + stack_trace;
            }

            return s;
        }

        public static string ToString(Exception exception)
        {
            var ce = new CustomException(exception);

            return ce.ToString();
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?