2013-09-27 3 views
0

메서드가 예외를 throw 할 때 ReadKey() 메서드가 작동하지 않습니다? 프로그램이 실행될 때 ReadKey 메서드는 메서드 dos가 예외를 throw하지 않는 경우에만 작동합니다. 메서드가 예외를 throw하면 콘솔 창이 두 번째 또는 두 번째로 나타납니다. 여기 응용 프로그램이 예외를 throw 할 때 ReadKey 메서드가 작동하지 않습니다.

은 방법 :

#region Using directives 
using System; 
#endregion 

namespace ParamsArray 
{ 
class Util 
{ 
    public static int Sum(params int[] paramList) 
    { 
     if (paramList == null) 
     { 
      throw new ArgumentException("Util.Sum: null parameter list"); 
     } 
     if (paramList.Length == 0) 
     { 
      throw new ArgumentException("Util.Sum: empty parameter list"); 
     } 

     int sumTotal = 0; 
     foreach (int i in paramList) 
     { 
      sumTotal += i; 
     } 

     return sumTotal; 
    } 
} 

} 여기

가 Program.cs

#region Using directives 

using System; 
using System.Collections.Generic; 
using System.Text; 

#endregion 

namespace ParamsArray 
{ 
    class Program 
    { 
     static void DoWork() 
     { 
      Console.WriteLine(Util.Sum(null)); 
      Console.ReadKey(); 
     } 

    static void Main() 
    { 
     try 
     { 
      DoWork(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Exception: {0}", ex.Message); 
     } 
    } 
} 

} 메소드가 예외를 throw하면

답변

2

, 그것은 당신이 때까지 작동이 중지 그 예외를 잡아라. Main 메서드에서만 예외가 catch되므로 Console.ReadKey() 메서드는 실행되지 않습니다. 실행되도록하려면 DoWork에 예외를 catch하거나 예외를 catch 한 후 또는 후에 ReadKey 메서드를 배치 할 수 있습니다.

관련 문제