본문 바로가기
개발/C#

유니티 C# 예외처리 Try Catch Finally 문 간단 사용법

by SPNK 2022. 6. 30.
반응형
  • Try Catch Finally 문을 사용하는 이유는?

프로그램 실행 중 예외상황이 발생하면 프로그램이 멈춰버리기 때문에 예외처리를 해줘서 멈추지 않게 만들어줍니다.

 

 

  • 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Example : MonoBehaviour
{
    int a = 0;
    int b = 0;

    int result = 0;

    private void Start()
    {
        try
        {
            result = a + b;

            Debug.Log(result);
        }
        catch (NullReferenceException e)
        {
            Debug.Log("오류 내용 : " + e);
        }
        finally
        {
            Debug.Log("오류가 발생해도 실행합니다.");
        }
    }
}
반응형

댓글