logo

Java에서 던지기와 던지기의 차이점

throw 및 throws는 throw 키워드가 메서드나 코드 블록에서 명시적으로 예외를 발생시키는 반면 throws 키워드는 메서드 서명에 사용되는 예외 처리의 개념입니다.

사이에는 많은 차이점이 있습니다. 던지다 그리고 던진다 키워드. 던지기와 던지기의 차이점 목록은 다음과 같습니다.

아니요. 차이점의 기초 던지다 던진다
1. 정의 Java throw 키워드는 코드, 함수 또는 코드 블록 내에서 명시적으로 예외를 발생시키는 데 사용됩니다. Java throws 키워드는 코드 실행 중에 함수에 의해 발생할 수 있는 예외를 선언하기 위해 메소드 서명에 사용됩니다.
2. 예외 유형 throw 키워드를 사용하면 확인되지 않은 예외만 전파할 수 있습니다. 즉, 확인된 예외는 throw만을 사용하여 전파될 수 없습니다. throws 키워드를 사용하면 확인된 예외와 확인되지 않은 예외를 모두 선언할 수 있습니다. 그러나 throws 키워드는 확인된 예외를 전파하는 데만 사용할 수 있습니다.
삼. 통사론 throw 키워드 뒤에는 던져질 Exception 인스턴스가 옵니다. throws 키워드 뒤에는 던져질 예외의 클래스 이름이 옵니다.
4. 선언 throw는 메서드 내에서 사용됩니다. throws는 메소드 서명과 함께 사용됩니다.
5. 내부 구현 한 번에 하나의 예외만 발생시킬 수 있습니다. 즉, 여러 개의 예외를 발생시킬 수 없습니다. 메서드에서 발생할 수 있는 throws 키워드를 사용하여 여러 예외를 선언할 수 있습니다. 예를 들어, main()은 IOException, SQLException을 발생시킵니다.

자바 던지기 예

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)>

산출:

Java에서 던지기와 던지기의 차이점

Java 던지기 및 던지기 예

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;); } } } 

산출:

Java에서 던지기와 던지기의 차이점