-
-
Save todorok1/cd5eab1c4aa6611d25c3cc7d035e706d to your computer and use it in GitHub Desktop.
パースする際のtry-catchのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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