2017-09-25 3 views
-4

System.Exception 클래스에서 모든 예외를 가질 수 있으므로 특정 예외 클래스를 사용하는 것이 가장 큰 이점은 무엇입니까? 특정 오류 처리 클래스를 사용하는 이유는 무엇입니까?C# 예외 특정 클래스 처리

+3

방금 ​​인터뷰에서 나왔습니까? –

+0

답변은 실제로 당신의 요구에 달려 있습니다 - 가끔 세분화가 필요한 경우가 있습니다. –

+3

가능한 경우 [우리 자신의 Java 예외 클래스를 만들어야합니까?] (https://stackoverflow.com/questions/22698584/when-should-we-create-our-own-java-exception-classes) –

답변

1

예외 처리기는 클래스 단위로 작동합니다. 당신이 단 하나 개의 예외 클래스가 있다면, 당신은이 작업을 수행 할 수 있습니다 : you should not catch the base exception가와 rethrowing를 기록 당신이하고있는 유일한 것은하지 않는 것을

try 
{ 
    //Do something that might raise different types of exceptions 
} 
catch(ArgumentException e1) //Catch any exception that is an ArgumentException or one its derived types 
{ 
    //Do something to handle the invalid argument 
} 
catch(NetworkException e2) //Catch any exception that is a NetworkException or one of its derived types 
{ 
    //Do something to handle the issue with the network 
} 
catch(Exception e3) 
{ 
    //Do something to log the unexpected exception 
    throw; 
} 

참고.