In Java, “throw” and “throws” are two different keywords with different meanings and usage.
“throw” is used to explicitly throw an exception from within a method or block of code. It is followed by an instance of an exception class that specifies the type of exception to be thrown. For example:
public void divide(int dividend, int divisor) { if (divisor == 0) { throw new ArithmeticException("Cannot divide by zero"); } int result = dividend / divisor; System.out.println(result); }
In the example above, the “throw” keyword is used to throw an instance of the “ArithmeticException” class when the “divisor” parameter is zero.
On the other hand, “throws” is used in a method signature to declare that the method may throw certain types of exceptions. It specifies the exception classes that the method might throw, separated by commas. For example:
public void readFile(String filename) throws FileNotFoundException, IOException { // Code to read a file }
In the example above, the “throws” keyword is used to declare that the “readFile” method may throw two types of exceptions: “FileNotFoundException” and “IOException”.
To summarize, “throw” is used to explicitly throw an exception, while “throws” is used to declare that a method may throw certain types of exceptions.
Java throw Example:
Sure, here’s an example of how to use the throw
keyword in Java to explicitly throw an exception:
public class DivideByZeroExample { public static void divide(int dividend, int divisor) { if (divisor == 0) { throw new ArithmeticException("Cannot divide by zero"); } int result = dividend / divisor; System.out.println(result); } public static void main(String[] args) { divide(10, 0); } }
In this example, the divide
method checks if the divisor
parameter is zero. If it is, the method throws an ArithmeticException
with the message “Cannot divide by zero”. If the divisor
parameter is not zero, the method calculates the result of dividing the dividend
by the divisor
, and prints the result to the console.
In the main
method, we call the divide
method with the parameters 10
and 0
. Since the divisor
is zero, the divide
method throws an ArithmeticException
, which is not caught in the divide
method. Instead, the exception propagates up the call stack until it is caught by the Java runtime system, which prints a stack trace to the console.
Here’s the output of running this example:
Exception in thread "main" java.lang.ArithmeticException: Cannot divide by zero at DivideByZeroExample.divide(DivideByZeroExample.java:5) at DivideByZeroExample.main(DivideByZeroExample.java:11)
Java throws Example:
Sure, here’s an example of how to use the throws
keyword in Java to declare that a method may throw certain types of exceptions:
import java.io.*; public class ReadFileExample { public static void readFile(String filename) throws FileNotFoundException, IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } public static void main(String[] args) { try { readFile("input.txt"); } catch (FileNotFoundException e) { System.err.println("File not found: " + e.getMessage()); } catch (IOException e) { System.err.println("IOException: " + e.getMessage()); } } }
In this example, the readFile
method reads the contents of a file and prints each line to the console. The method declares that it may throw two types of exceptions: FileNotFoundException
and IOException
. This is done in the method signature by adding the throws
keyword followed by the exception types, separated by commas.
In the main
method, we call the readFile
method with the filename “input.txt”. Since the readFile
method may throw a FileNotFoundException
or an IOException
, we wrap the call to readFile
in a try-catch block that catches these exceptions separately. If a FileNotFoundException
is thrown, we print an error message indicating that the file was not found. If an IOException
is thrown, we print an error message indicating that an I/O error occurred.
Here’s an example of the contents of the input file:
Hello World
Here’s the output of running this example:
Hello World
Java throw and throws Example:
Sure, here’s an example that shows the difference between the throw
and throws
keywords in Java:
import java.io.*; public class ReadFileExample { public static void readFile(String filename) throws FileNotFoundException, IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } public static void main(String[] args) { try { // This line of code will throw a FileNotFoundException readFile("input.txt"); // This line of code will throw an ArithmeticException int x = 5 / 0; } catch (FileNotFoundException e) { System.err.println("File not found: " + e.getMessage()); } catch (IOException e) { System.err.println("IOException: " + e.getMessage()); } catch (ArithmeticException e) { System.err.println("ArithmeticException: " + e.getMessage()); } } }
In this example, the readFile
method reads the contents of a file and prints each line to the console. The method declares that it may throw two types of exceptions: FileNotFoundException
and IOException
. This is done in the method signature by adding the throws
keyword followed by the exception types, separated by commas.
In the main
method, we call the readFile
method with the filename “input.txt”. Since the readFile
method may throw a FileNotFoundException
or an IOException
, we wrap the call to readFile
in a try-catch block that catches these exceptions separately.
After calling the readFile
method, we have a line of code that will throw an ArithmeticException
by dividing the integer 5
by 0
. This exception is not declared in the method signature using the throws
keyword, because it is a runtime exception and does not need to be declared.
In the catch blocks, we print error messages indicating the type of exception that was caught and its message.
Here’s an example of the contents of the input file:
Hello World
Here’s the output of running this example:
Hello World ArithmeticException: / by zero
As you can see, the readFile
method uses the throws
keyword to declare that it may throw certain types of exceptions. The main
method uses the try-catch
block to catch the declared exceptions and handle them appropriately. The ArithmeticException
is not declared in the method signature, because it is a runtime exception that does not need to be declared. Instead, it is caught in a separate catch block.