2011-02-09 25 views
2

vs2010でいくつかの練習をしています。ThrowExceptionは現在のコンテキストに存在しません

/ようやくのtry/catchで作業し、次のメッセージ取得:

投げる例外は現在のコンテキスト内に存在しませんが。 ThrowExcpetionを使用するためにusingステートメントがありませんか?あなたは構文が意図的に例外をスローするために

throw new Exception(eType); 
+0

ThrowException()とは何ですか?これは組み込みメソッドではなく、どこで定義しますか? – BoltClock

+0

例外を処理する方法を学習する場合は、ex.Messageを表示しないように学習します。例外全体を取得するには、ex.ToString()を使用します。 –

答えて

0

単に

throw new WhateverException(yourMessage, yourInnerException); 

ある典型的な例外はまた、コンストラクタはメッセージのみを受け入れるかのどちらかというオーバーロードがありますパラメータはまったくありません。

0

を使用していないのはなぜ

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

class Program 
{ 
    static string[] eTypes = { "none", "simple", "index", "nested index" }; 
    static void Main(string[] args) 
    { 
     foreach (string eType in eTypes) 
     { 
      try 
      { 
       Console.WriteLine("Main() try"); 
       Console.WriteLine("Throwing: \"{0}\") Called.",eType); 

       ThrowException(eType); 
       Console.WriteLine("Main() try continuing"); 

      } 
      catch (System.IndexOutOfRangeException e) 
      { 
       Console.WriteLine("Main System.indexoutofrange catch", 
        e.Message); 
      } 
      finally 
      { 
       Console.WriteLine("Main finally"); 
      } 
      Console.WriteLine(); 
     } 
     Console.ReadKey(); 

    } 
} 
3

ThrowExceptionというメソッドが定義されていません。

例外を発生させようとしている場合は、それがどのように行われたかではありません。あなたがしたい:

throw new SomeTypeOfException(eType); 

は、あなたのテストを考えると、私はあなたが欲しい疑う:詳細については

throw new IndexOutOfRangeException(eType); 

は、throw (C# Reference)を参照してください。

1

ThrowException()関数ではありません。その声明。新しい例外( "some message")を投げる。

関連する問題