Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created December 2, 2020 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/cd5eab1c4aa6611d25c3cc7d035e706d to your computer and use it in GitHub Desktop.
Save todorok1/cd5eab1c4aa6611d25c3cc7d035e706d to your computer and use it in GitHub Desktop.
パースする際のtry-catchのサンプル
using UnityEngine;
using System;
public class HandlingErrorTest : MonoBehaviour
{
void Start()
{
try
{
string num = "123";
int stringNumber = int.Parse(num);
Debug.Log($"変換された値は{stringNumber}でした。");
}
catch (ArgumentNullException e)
{
// 引数がnullだった場合の例外処理を書きます。
Debug.Log(e.Message);
Debug.Log("パースする文字列がnullです。");
}
catch (FormatException e)
{
// 引数の書式が指定したもの(デフォルトではint)ではないものだった場合の例外処理を書きます。
Debug.Log(e.Message);
Debug.Log("書式がおかしいです。");
}
catch (OverflowException e)
{
// 引数の数値がオーバーフローする場合の例外処理を書きます。
Debug.Log(e.Message);
Debug.Log("オーバーフローしちゃいました。");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment