logo

Unterschied zwischen throw und throws in Java

Bei „throw“ und „throws“ handelt es sich um das Konzept der Ausnahmebehandlung, bei dem das Schlüsselwort „throw“ die Ausnahme explizit aus einer Methode oder einem Codeblock auslöst, während das Schlüsselwort „throws“ in der Signatur der Methode verwendet wird.

Es gibt viele Unterschiede zwischen werfen Und wirft Schlüsselwörter. Nachfolgend finden Sie eine Liste der Unterschiede zwischen Wurf und Würfen:

Herr Nr. Grundlage der Unterschiede werfen wirft
1. Definition Das Java-Schlüsselwort throw wird verwendet, um eine Ausnahme explizit im Code, innerhalb der Funktion oder des Codeblocks auszulösen. Das Schlüsselwort „Java throws“ wird in der Methodensignatur verwendet, um eine Ausnahme zu deklarieren, die von der Funktion während der Ausführung des Codes ausgelöst werden könnte.
2. Art der Ausnahme Mit dem Schlüsselwort „throw“ können wir nur ungeprüfte Ausnahmen weitergeben, d. h. die geprüfte Ausnahme kann nicht nur mit „throw“ weitergegeben werden. Mit dem Schlüsselwort throws können wir sowohl aktivierte als auch nicht aktivierte Ausnahmen deklarieren. Das Schlüsselwort throws kann jedoch nur zur Weitergabe geprüfter Ausnahmen verwendet werden.
3. Syntax Auf das Schlüsselwort throw folgt eine Instanz von Exception, die ausgelöst werden soll. Auf das Schlüsselwort throws folgen die Klassennamen der auszulösenden Ausnahmen.
4. Erklärung throw wird innerhalb der Methode verwendet. throws wird mit der Methodensignatur verwendet.
5. Interne Umsetzung Wir dürfen jeweils nur eine Ausnahme auslösen, d. h. wir können nicht mehrere Ausnahmen auslösen. Mit dem Schlüsselwort throws können wir mehrere Ausnahmen deklarieren, die von der Methode ausgelöst werden können. Beispielsweise löst main() eine IOException oder eine SQLException aus.

Beispiel für einen Java-Wurf

TestThrow.java

 public class TestThrow { //defining a method public static void checkNum(int num) { if (num <1) { throw new arithmeticexception('
number is negative, cannot calculate square'); } else system.out.println('square of ' + num (num*num)); main method public static void main(string[] args) testthrow obj="new" testthrow(); obj.checknum(-3); system.out.println('rest the code..'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/exception-handling/22/difference-between-throw.webp" alt="Difference between throw and throws in Java"> <h2>Java throws Example</h2> <p> <strong>TestThrows.java</strong> </p> <pre> public class TestThrows { //defining a method public static int divideNum(int m, int n) throws ArithmeticException { int div = m / n; return div; } //main method public static void main(String[] args) { TestThrows obj = new TestThrows(); try { System.out.println(obj.divideNum(45, 0)); } catch (ArithmeticException e){ System.out.println(&apos;
Number cannot be divided by 0&apos;); } System.out.println(&apos;Rest of the code..&apos;); } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/exception-handling/22/difference-between-throw-2.webp" alt="Difference between throw and throws in Java"> <h2>Java throw and throws Example</h2> <p> <strong>TestThrowAndThrows.java</strong> </p> <pre> public class TestThrowAndThrows { // defining a user-defined method // which throws ArithmeticException static void method() throws ArithmeticException { System.out.println(&apos;Inside the method()&apos;); throw new ArithmeticException(&apos;throwing ArithmeticException&apos;); } //main method public static void main(String args[]) { try { method(); } catch(ArithmeticException e) { System.out.println(&apos;caught in main() method&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/exception-handling/22/difference-between-throw-3.webp" alt="Difference between throw and throws in Java"> <hr></1)>

Ausgabe:

Unterschied zwischen throw und throws in Java

Beispiel für Java-Wurf und -Würfe

TestThrowAndThrows.java

 public class TestThrowAndThrows { // defining a user-defined method // which throws ArithmeticException static void method() throws ArithmeticException { System.out.println(&apos;Inside the method()&apos;); throw new ArithmeticException(&apos;throwing ArithmeticException&apos;); } //main method public static void main(String args[]) { try { method(); } catch(ArithmeticException e) { System.out.println(&apos;caught in main() method&apos;); } } } 

Ausgabe:

Unterschied zwischen throw und throws in Java