ホームページ  >  記事  >  バックエンド開発  >  C# の例外の種類

C# の例外の種類

PHPz
PHPzオリジナル
2024-09-03 15:20:36811ブラウズ

プログラムの実行中に発生する問題は例外であり、これらの例外は、ゼロで除算しようとして制御が移るたびに発生する例外のような、プログラムの実行中に例外的な状況に対する応答です。例外を介してプログラムのある部分からプログラムの別の部分に移動し、例外の処理は C# の 4 つのキーワード (try、catch、finally、throw ブロック) で管理されます。

C# の例外の種類と例

C# にはいくつかの種類の例外があります。それらは次のとおりです:

1. System.OutOfMemoryException

空きメモリ不足により生成されるエラーは、この例外によって処理されます。システムを説明するために、以下のプログラム例を考えてみましょう。 OutOfMemoryException.

例:

//a class called check is defined
public class check
{
//main method is called
public static void Main()
{
// a string variable is created and tried to store 2.1 billion characters and this causes an out of memory exception
string val = new string('r', int.MaxValue);
}
}

出力:

C# の例外の種類

出力:

上記のプログラムでは、checkというクラスが定義されています。次に、main メソッドが呼び出されます。文字列変数が作成され、21 億文字を保存しようとすると、メモリ不足例外が発生します。

2. System.NullReferenceException

null オブジェクトの参照によって生成されたエラーは、この例外によって処理されます。システムを説明するために、以下のプログラム例を考えてみましょう。 NullReferenceException

:

using System;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
//a string variable is defined, and it is referencing to null
string value = null;
//the length of the value referencing to null is checked if it is equal to zero causing an exception
if (value.Length == 0)
{
Console.WriteLine(value);
}
}
}

出力:

C# の例外の種類

上記のプログラムでは、checkというクラスが定義されています。次に、main メソッドが呼び出されます。次に、文字列変数が定義され、null を参照しています。次に、null を参照する値の長さがチェックされ、それがゼロに等しいかどうかがチェックされ、例外が発生します。

3. System.InvalidCastException

型キャスト中に生成されたエラーは、この例外によって処理されます。システムを説明するために、以下のプログラム例を考えてみましょう。 InvalidCastException.

例:

using System.IO;
using System.Text;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
// an instance of the string builder class is created which is then assigned to a new object through implicit casting and then casting is tried explicitly to convert the instance of stringbuilder class to streamreader class
StringBuilder ref1 = new StringBuilder();
object ref2 = ref1;
StreamReader ref3 = (StreamReader)ref2;
}
}

出力:

C# の例外の種類

上記のプログラムでは、checkというクラスが定義されています。次に、main メソッドが呼び出されます。次に、string Builder クラスのインスタンスが作成され、暗黙的なキャストを通じて新しいオブジェクトに割り当てられます。その後、stringbuilder クラスのインスタンスを streamreader クラスに変換するために明示的にキャストが試行され、例外が発生します。

4. System.ArrayTypeMismatchException

配列の型と型が一致しない場合に生成されるエラーは、この例外によって処理されます。システムを説明するために、以下のプログラム例を考えてみましょう。 ArrayTypeMismatchException.

例:

//a class called check is defined
class check
{
//main method is called
static void Main()
{
// a string is defined and assigned the values which is then assigned to object class array and then an integer is tried to put in the same array which causes an exception
string[] arr1 = { "Welcome", "to", "CSharp" };
object[] arr2 = arr1;
arr2[0] = 8;
}
}

出力:

C# の例外の種類

上記のプログラムでは、checkというクラスが定義されています。次に main メソッドを定義します。次に、文字列が定義されて値が割り当てられ、その値がオブジェクト クラスの配列に割り当てられ、同じ配列に整数を入れようとすると例外が発生します。

5. System.IndexOutOfRangeException

メソッドが範囲外の配列を参照しているときに生成されるエラーは、この例外によって処理されます。システムを説明するために、以下のプログラム例を考えてみましょう。 IndexOutOfRangeException.

例:

//a class called check is defined
class check
{
//main method is called
static void Main()
{
// an array is defined to store 100 integers but then an integer is tried to be stores at a position outside of the size of the array which causes an exception
int[] arr = new int[10];
arr[0] = 10;
arr[10] = 20;
arr[20] = 30;
}
}

出力:

C# の例外の種類

上記のプログラムでは、checkというクラスが定義されています。次に、main メソッドが呼び出されます。次に、100 個の整数を格納する配列が定義されていますが、配列のサイズを超えた位置に整数を格納しようとすると、例外が発生します。

6. System.DivideByZeroException

被除数をゼロで割ったときに生成されるエラーは、この例外によって処理されます。システムを説明するために、以下のプログラム例を考えてみましょう。 DivideByZeroException.

例:

using System;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
//an integer variable res is defined which is tried to divide by zero which causes an exception
int res = 10 / int.Parse("0");
Console.WriteLine(res);
}
}

出力:

C# の例外の種類

上記のプログラムでは、checkというクラスが定義されています。次に、main メソッドが呼び出されます。次に、整数変数 res が定義され、ゼロで除算しようとすると例外が発生します。

7. System.StackOverflowException

スタックのオーバーフローによって生成されたエラーは、この例外によって処理されます。システムを説明するために、以下のプログラム例を考えてみましょう。 StackOverflowException.

例:

using System;
//a class called check is defined
public class check
{
// a method called recurse is defined which takes a value as parameter and increases its value by one
static void Recurse(int val)
{
// since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing exception
Console.WriteLine(val);
Recurse(++val);
}
//main method is called
public static void Main()
{
//The recurse method is called to start the infinite recursion
Recurse(0);
}
} <strong>Output:</strong>

C# の例外の種類

In the above program, a class called check is defined. Then a method called recurse is defined which takes a value as a parameter and increases its value by one. Then the main method is called in which the infinite loop for recursion begins by passing zero as a parameter. Then since we have written a recursive loop and 0 is passed as a parameter, it ends in an infinite loop causing an exception.

8. System.IO.IOException

The errors that are generated by input, the output is handled by this exception. Consider the below example program to demonstrate System. IO. IOException.

Example:

using System;
using System.IO;
//a class called check is defined
class check
{
//main methos is called
static void Main()
{
try
{
//a file is tried to open which do not exist and causes an exception
File.Open("D:\\ex.txt", FileMode.Open);
}
catch (IOException)
{
Console.WriteLine("Inputoutput Exception is handled");
}
}
}

Output:

C# の例外の種類

In the above program, a class called check is defined. Then the main method is called. Then a file is tried to open which does not exist and causes an exception.

以上がC# の例外の種類の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:C# での例外処理次の記事:C# での例外処理