유니티로 배우는 c#

예외 처리

테오구 2021. 10. 11. 23:39
728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


public class NewBehaviourScript : MonoBehaviour
{
    int a = 5;
    int b = 0;
    int c;

    void Start()
    {
        try // 예외를 잡아주기 위해
        {
            c = a / b;
        }
        catch (DivideByZeroException ie)
        {
            print(ie);
            b = 1;
            print(b);
            c = a / b;
            print(c);
        }

        catch (NullReferenceException ie)//아무것도 없을 때 발생하는 오류
        { // 오류 원인마다 처리할 수 있다.
            print(ie);
            b = 1;
            print(b);
            c = a / b;
            print(c);
        }

        finally // 오류가 발생하던 발생하지 않던 사용됩니다.
        {
            print(c);
        }

        throw new Exception("일부러 오류를 발생시킴");// 일부러 오류를 던지는 아이
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
728x90

'유니티로 배우는 c#' 카테고리의 다른 글

코루틴  (0) 2021.10.12
Action과 Func  (0) 2021.10.11
람다식  (0) 2021.10.11
형식 매개 변수 T  (0) 2021.10.10
인터페이스  (0) 2021.10.10