728x90
반응형
using System;
using System.Collections;
class Program
{
public static void Main(string[] args)
{
Console.Write("나눌 숫자를 입력하세요 : ");
int divider = int.Parse(Console.ReadLine());
Console.WriteLine(10 / divider);
}
}
0을 입력하면
Unhadled exception~~~ 이렇게 나옴...
그래서 try catch 문을 쓰면된다.
using System;
using System.Collections;
class Program
{
public static void Main(string[] args)
{
Console.Write("나눌 숫자를 입력하세요 : ");
int divider = int.Parse(Console.ReadLine());
try
{
Console.WriteLine(10 / divider);
}
catch
{
Console.WriteLine("0으로 나눌수 없습니다.");
}
}
}
예외처리 문장에서 에러난 상황을 알고 싶을때는
try catch(Exception e)를 쓰면 된다.
using System;
using System.Collections;
class Program
{
public static void Main(string[] args)
{
Console.Write("나눌 숫자를 입력하세요 : ");
int divider = int.Parse(Console.ReadLine());
try
{
Console.WriteLine(10 / divider);
}
catch (Exception e)
{
Console.WriteLine("예외" + e.Message);
}
}
}
728x90
반응형
'c#' 카테고리의 다른 글
C# [14] 로그인 창 만들기 (0) | 2025.04.08 |
---|---|
C# [13] Winform (0) | 2025.04.08 |
C# [11] ArrayList, Queue, Stack, Hashtable (0) | 2025.04.08 |
C# [10] 데이터 저장을 위한 배열 클래스의 개념& 배열 초기화, 값 저장, 길이 출력 실습 & foreach 문을 통한 배열의 값 출력하기 실습 (0) | 2025.04.08 |
C# [9] MSDN & 닷넷 편집기로 코드 실행하기 & 속성 및 메서드 찾기 (0) | 2025.04.07 |