Throw and Throws

I am learning Exception Handling in Java and stuck with Throw and Throws As I am not getting when to use throw and when to use throws.
17 Replies
JavaBot
JavaBot5d ago
This post has been reserved for your question.
Hey @Dev Gupta! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Dev Gupta
Dev GuptaOP5d ago
Help Please
ayylmao123xdd
ayylmao123xdd5d ago
throw is used when you want to throw an exception for example
public void checkIfEqualsFive(int number) {
if (number != 5) {
throw new IllegalArgumentException("number must be five")
}
}
public void checkIfEqualsFive(int number) {
if (number != 5) {
throw new IllegalArgumentException("number must be five")
}
}
throws is used when a method throws a checked exception so you gotta either use try catch or the throws thing like this
public void readFile(String filePath) throws IOException {
//code that reads a file
}
public void readFile(String filePath) throws IOException {
//code that reads a file
}
if you use try catch you dont need the throws in the method because the exception is handled within that try catch or try with resources
Dev Gupta
Dev GuptaOP5d ago
Ok let me do it once and get clear @ayylmao123xdd can you give example for second code
ayylmao123xdd
ayylmao123xdd5d ago
try catch or throws
Dev Gupta
Dev GuptaOP5d ago
try catch
ayylmao123xdd
ayylmao123xdd5d ago
ok but in like 1h or 2 cuz now im doing some stuff
Dev Gupta
Dev GuptaOP5d ago
Ok
import java.util.Scanner;

public class Test extends Object{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
checkIfEqualsFive(n);
sc.close();
}

public static void checkIfEqualsFive(int number) {
if (number != 5) {
throw new IllegalArgumentException("number must be five");
}
}
}
import java.util.Scanner;

public class Test extends Object{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
checkIfEqualsFive(n);
sc.close();
}

public static void checkIfEqualsFive(int number) {
if (number != 5) {
throw new IllegalArgumentException("number must be five");
}
}
}
Check is this correct or the other one for throw keyword
import java.util.Scanner;

public class Test extends Object{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try{
int n=sc.nextInt();
checkIfEqualsFive(n);
}
catch(IllegalArgumentException e){
System.out.println(e);
}
sc.close();
}

public static void checkIfEqualsFive(int number) {
if (number != 5) {
throw new IllegalArgumentException("number must be five");
}
}
}
import java.util.Scanner;

public class Test extends Object{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try{
int n=sc.nextInt();
checkIfEqualsFive(n);
}
catch(IllegalArgumentException e){
System.out.println(e);
}
sc.close();
}

public static void checkIfEqualsFive(int number) {
if (number != 5) {
throw new IllegalArgumentException("number must be five");
}
}
}
Or is this correct for the throw keyword?
ayylmao123xdd
ayylmao123xdd5d ago
this one is good yea this one also works but you could just do like this
if (number != 5) {
System.out.println("number must be five");
}
if (number != 5) {
System.out.println("number must be five");
}
because you are printing it anyway and with try catch let me write it now
Dev Gupta
Dev GuptaOP5d ago
This how be use throw keyword?
ayylmao123xdd
ayylmao123xdd5d ago
public void readFile(String filePath) {
try {
//code that reads a file
} catch (IOException e) {
// do stuff with the exception
}
}
public void readFile(String filePath) {
try {
//code that reads a file
} catch (IOException e) {
// do stuff with the exception
}
}
and then you dont need the throws yes
Dev Gupta
Dev GuptaOP5d ago
Ok, thank you for clearing the concept
ayylmao123xdd
ayylmao123xdd5d ago
heres a gif for u l0l and u should try with other examples too so you remember how to do it
Dev Gupta
Dev GuptaOP4d ago
Ok
dan1st
dan1st4d ago
Maybe the following is simple and explains it (if there are any doubts left): - These are different keywords and you cannot use one instead of the other (or you'll get a compiler error) - throw is for the act of throwing an exception - throws makes a statement that a method might be throwing an (uncaught) exception. Calling another method that might throw an exception without catching it constitutes as such. and you should catch exceptions wherever you feel it's sensible to handle the situation of these exceptions occuring and you always have the option of catching an exception and throwing another one if you want to change the type of the exception
JavaBot
JavaBot4d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?