Dart-programming-exceptions
Dartプログラミング-例外
例外(または例外的なイベント)は、プログラムの実行中に発生する問題です。 例外が発生すると、プログラムの通常のフローが中断され、プログラム/アプリケーションが異常終了します。
組み込みのDartの例外には次のものがあります-
Sr.No | Exceptions & Description |
---|---|
1 |
DeferredLoadException 遅延ライブラリがロードに失敗するとスローされます。 |
2 |
FormatException 文字列またはその他のデータに予期される形式がなく、解析または処理できない場合にスローされる例外。 |
3 |
IntegerDivisionByZeroException 数値がゼロで除算されるとスローされます。 |
4 |
IOException Inupt-Outputに関連するすべての例外の基本クラス。 |
5 |
IsolateSpawnException 分離を作成できない場合にスローされます。 |
6 |
Timeout 非同期結果を待っている間にスケジュールされたタイムアウトが発生したときにスローされます。 |
Dartのすべての例外は、事前定義されたクラス Exception のサブタイプです。 アプリケーションが突然終了しないように、例外を処理する必要があります。
try/on/catchブロック
*try* ブロックには、例外が発生する可能性のあるコードが埋め込まれます。 onブロックは、例外タイプを指定する必要がある場合に使用されます。 *catch* ブロックは、ハンドラーが例外オブジェクトを必要とするときに使用されます。
*try* ブロックの後には、正確に1つの *on/catch* ブロックまたは1つの *finally* ブロック(またはその両方)が続く必要があります。 tryブロックで例外が発生すると、制御は *catch* に転送されます。
例外を処理するための*構文*は以下のとおりです-
try {
//code that might throw an exception
}
on Exception1 {
//code for handling exception
}
catch Exception2 {
//code for handling exception
}
以下は覚えておくべきポイントです-
- コードスニペットは、複数の例外を処理するために複数のon/catchブロックを持つことができます。
- onブロックとcatchブロックは相互に包括的です。 tryブロックは、onブロックとcatchブロックの両方に関連付けることができます。
次のコードは、Dartでの例外処理を示しています-
例:ONブロックの使用
次のプログラムは、変数 x および y でそれぞれ表される2つの数値を除算します。 ゼロによる除算を試みるため、コードは例外をスローします。 on block には、この例外を処理するコードが含まれています。
main() {
int x = 12;
int y = 0;
int res;
try {
res = x ~/y;
}
on IntegerDivisionByZeroException {
print('Cannot divide by zero');
}
}
次の output が生成されるはずです-
Cannot divide by zero
例:catchブロックの使用
次の例では、上記と同じコードを使用しています。 唯一の違いは、(ONブロックではなく)* catchブロック*に例外を処理するコードが含まれていることです。 catch のパラメーターには、実行時にスローされる例外オブジェクトが含まれます。
main() {
int x = 12;
int y = 0;
int res;
try {
res = x ~/y;
}
catch(e) {
print(e);
}
}
次の output が生成されるはずです-
IntegerDivisionByZeroException
例:on…catch
次の例は、 on … catch ブロックの使用方法を示しています。
main() {
int x = 12;
int y = 0;
int res;
try {
res = x ~/y;
}
on IntegerDivisionByZeroException catch(e) {
print(e);
}
}
次の output が生成されるはずです-
IntegerDivisionByZeroException
最後のブロック
*finally* ブロックには、例外の発生に関係なく実行されるコードが含まれています。 オプションの *finally* ブロックは、 *try/on/catch* の後に無条件に実行されます。
*finally* ブロックを使用するための構文は次のとおりです-
try {
//code that might throw an exception
}
on Exception1 {
//exception handling code
}
catch Exception2 {
// exception handling
}
finally {
//code that should always execute; irrespective of the exception
}
次の例は、 finally ブロックの使用法を示しています。
main() {
int x = 12;
int y = 0;
int res;
try {
res = x ~/y;
}
on IntegerDivisionByZeroException {
print('Cannot divide by zero');
}
finally {
print('Finally block executed');
}
}
次の output が生成されるはずです-
Cannot divide by zero
Finally block executed
例外を投げる
*throw* キーワードは、明示的に例外を発生させるために使用されます。 発生した例外は、プログラムが突然終了するのを防ぐために処理する必要があります。
明示的に例外を発生させるための*構文*は-
throw new Exception_name()
例
次の例は、 throw キーワードを使用して例外をスローする方法を示しています-
main() {
try {
test_age(-2);
}
catch(e) {
print('Age cannot be negative');
}
}
void test_age(int age) {
if(age<0) {
throw new FormatException();
}
}
次の output が生成されるはずです-
Age cannot be negative
カスタム例外
上記で指定したように、Dartのすべての例外タイプは、組み込みクラス Exception のサブタイプです。 Dartでは、既存の例外を拡張することにより、カスタム例外を作成できます。 カスタム例外を定義するための構文は以下のとおりです-
構文:例外の定義
class Custom_exception_Name implements Exception {
//can contain constructors, variables and methods
}
カスタム例外は明示的に発生させる必要があり、同じ例外をコードで処理する必要があります。
例
次の例は、カスタム例外を定義および処理する方法を示しています。
class AmtException implements Exception {
String errMsg() => 'Amount should be greater than zero';
}
void main() {
try {
withdraw_amt(-1);
}
catch(e) {
print(e.errMsg());
}
finally {
print('Ending requested operation.....');
}
}
void withdraw_amt(int amt) {
if (amt <= 0) {
throw new AmtException();
}
}
上記のコードでは、カスタム例外 AmtException を定義しています。 渡された金額が例外範囲内にない場合、コードは例外を発生させます。 main 関数は、 try … catch ブロックで関数呼び出しを囲みます。
コードは、次の*出力*を生成する必要があります-
Amount should be greater than zero
Ending requested operation....