Difference between try-catch and throw in java
Solution 1:
- The
try
block will execute a sensitive code which can throw exceptions - The
catch
block will be used whenever an exception (of the type caught) is thrown in the try block - The
finally
block is called in every case after the try/catch blocks. Even if the exception isn't caught or if your previous blocks break the execution flow. - The
throw
keyword will allow you to throw an exception (which will break the execution flow and can be caught in acatch
block). - The
throws
keyword in the method prototype is used to specify that your method might throw exceptions of the specified type. It's useful when you have checked exception (exception that you have to handle) that you don't want to catch in your current method.
Resources :
- oracle.com - Lesson: Exceptions
On another note, you should really accept some answers. If anyone encounter the same problems as you and find your questions, he/she will be happy to directly see the right answer to the question.
Solution 2:
If you execute the following example, you will know the difference between a Throw and a Catch block.
In general terms:
The catch block will handle the Exception
throws will pass the error to his caller.
In the following example, the error occurs in the throwsMethod() but it is handled in the catchMethod().
public class CatchThrow {
private static void throwsMethod() throws NumberFormatException {
String intNumber = "5A";
Integer.parseInt(intNumber);
}
private static void catchMethod() {
try {
throwsMethod();
} catch (NumberFormatException e) {
System.out.println("Convertion Error");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
catchMethod();
}
}
Solution 3:
Try/catch and throw clause are for different purposes. So they are not alternative to each other but they are complementary.
If you have throw some checked exception in your code, it should be inside some try/catch in codes calling hierarchy.
Conversely, you need try/catch block only if there is some throw clause inside the code (your code or the API call) that throws checked exception.
Sometimes, you may want to throw exception if particular condition occurred which you want to handle in calling code block and in some cases handle some exception catch block and throw a same or different exception again to handle in calling block.
Solution 4:
All these keywords try, catch and throw are related to the exception handling concept in java. An exception is an event that occurs during the execution of programs. Exception disrupts the normal flow of an application. Exception handling is a mechanism used to handle the exception so that the normal flow of application can be maintained. Try-catch block is used to handle the exception. In a try block, we write the code which may throw an exception and in catch block we write code to handle that exception. Throw keyword is used to explicitly throw an exception. Generally, throw keyword is used to throw user defined exceptions.
For more detail visit Java tutorial for beginners.