ホームページ  >  記事  >  Java  >  Java のスローキーワード

Java のスローキーワード

WBOY
WBOYオリジナル
2024-08-30 15:22:07349ブラウズ

Java throws はメソッドの名前で宣言されるキーワードであり、メソッドが呼び出されるときに例外名が発生する可能性があります。キーワードを宣言すると、例外処理に役立ち、特定のメソッドがチェック例外をスローすることをコンパイラに知らせます。チェック例外は、IOException や ClassNotFoundException など、コンパイル時に宣言する必要があります。 try-catch ブロックまたは throws キーワードを使用してチェック例外を処理しない場合、コンパイラはコンパイル時エラーをスローします。このキーワードは、プログラマがメソッドが例外をスローすることを認識できるため、効率的なアプリケーションを開発するのにも役立ちます。

構文:

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

Access_specifierreturn_typemethod_namethrowsexception_name

こちら

  • Access_specifier: これは、特定のメソッドにアクセスできる場所を JVM に指示するキーワードを指します。例: プライベート/パブリック、または保護。
  • Return_type: これは、呼び出されたメソッドによって返されるオブジェクトのデータ型 (int、Boolean、String など) を示すキーワードを指します。
  • Method_name: これは、呼び出す必要があるメソッドの名前を指します。
  • Exception_name には、プログラム フローの実行中にプロセスが発生する可能性のある組み込み例外またはカスタム例外を指定できます。

例:

public void my_method() throws MyException

Java では Throws キーワードはどのように機能しますか?

エラーと例外は、Java アプリケーションにとって非常に重要です。これは、構文エラーや入力された入力がデータと一致しないなど、何か不適切なことが発生したかどうかを判断するのに役立ちます。さらに、エラーは避けられず、コンパイル エラーを引き起こしたり、アプリケーションの実行が予期せず停止したりするだけです。例外はある程度処理できます。

Java のスローキーワード

例外の処理とは、例外が発生した場合に、正しく決定された方法で実行を停止する方法を意味します。

例外には 2 種類あります:

  • 未チェック例外: これらのタイプの例外は、コード内で処理される場合にコンパイラーによってチェックされない実行時例外です。例 - 算術例外、IndexOutOfBoundsException など。これらの例外に対して throws キーワードを使用しても意味がありません。
  • チェックされた例外: これらは、コンパイラーがコンパイル時に処理されるかどうかをチェックする例外のタイプです。したがって、これらが処理されない場合、コンパイラはエラー「未処理の例外タイプ」をスローします。例 – IOException、ClassNotFoundException.

2 通りのハンドル

したがって、チェック例外を処理するには 2 つの方法があります。

1.試し釣り

try-catch を使用すると、例外を発生させる可能性のあるステートメントを try ブロックに配置し、例外が発生した場合には、制御が catch ブロック内のステートメントに移ってそれらを実行します。このようにして、例外が発生したときのアプリケーションのフローを制御できます。

コード:

//package Proc;
class MyCustomeException extends Throwable{
MyCustomeException(String s){
super(s);
}
}
public class prac1
{
public static void main(String[] args)    {
try{
System.out.println("Now exception is being raised");
throw new MyCustomeException("Custom exception is thrown");
}
catch(MyCustomeException e){
System.out.println("Here exception is caught and handled");
}
}
}

出力:

Java のスローキーワード

説明: 上記の例では、カスタム例外を宣言し、throw キーワードを使用して main メソッドで明示的に例外をスローしました。コントロールがメソッド コントロールに入り例外をスローすると、直接 catch ブロックに進み、それらのステートメントを実行してプログラムを終了します。

2.キーワードをスローします

このキーワードをメソッド名で宣言すると、メソッドが例外をスローできることがコンパイラに伝えられます。このキーワードは、コード内で意図的に例外をスローするために、またはカスタム例外を操作する際に使用される throw キーワードとよく混同されます。

  • キーワードのみをスローすると、例外が発生した場合にステートメントを実行できます。例外の発生を回避するためには使用できません。したがって、例外処理に使用されます。
  • throws キーワードは、明示的に例外をスローするために使用される throw とよく混同されます。そして、それを処理するためにスローが使用されます。
  • 例外が発生した場合、行キーワードはプログラマがプログラムをスムーズかつ効率的に実行するのに役立ちます。

Java での Throws キーワードの実装例

throws キーワードは 2 つの方法で使用できます:

例 #1

まず、例外がスローされるメソッドの宣言で throws を使用できます。

コード:

//package Proc;
public class prac1
{
public static void main(String[] args)throws IllegalAccessException
{
System.out.println("Hello Everyone lets start our learning with throws keyword");
throw new IllegalAccessException("My Exception");
}
}

出力:

Java のスローキーワード

Explanation: In the above example, we have used the throws keyword to handle the exception being thrown using the throw keyword. In case an exception is raised, it will not prevent the program’s abnormal termination; instead, it helps to convince the compiler. Also, it helps to inform the programmer that the method might throw an exception and needs exception handling.

Example #2

Second, we use a try-catch block while calling a method that is likely to throw an exception. We have made a custom exception here named MyCustomeException that is being thrown by the method throwsDemo.

Code:

//package Proc;
class MyCustomeException extends Throwable{
MyCustomeException(String s){
super(s);
}
}
public class prac1
{
static void throwsDemo() throws MyCustomeException
{
System.out.println("We are Inside the method");
throw new MyCustomeException("Custom exception is thrown");
}
public static void main(String[] args)throws IllegalAccessException
{
try{
System.out.println("Now exception is being raised");
throwsDemo();
}
catch(MyCustomeException e){
System.out.println("Here exception is caught and handled");
}
}
}

Output:

Java のスローキーワード

Explanation: In the above example, one method throws a new exception. This is indicated using the throws keyword. But when the method is called in the main method, we can use a try-catch block or declaring a throws keyword with the main method to handle the exception.

Conclusion

Throws keyword is used for exception handling in Java, where one needs to handle the flow of the program when a checked exception occurs. This is different from the throw keyword and must only be used with the checked exception since it does not prevent the occurrence of an exception but helps the way that must execute if one occurs.

以上がJava のスローキーワードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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