Java Keywords (Part XI): Throwing Exceptions
This article will illustrate the use of the keywords throw and throws, in Java Exception Handling. It will not get into specific usages of Exception Handling. For that, please go to my article covering this topic. Also, be on the lookout for a new article covering other facets of Java Exception Handling, such as "try with resources."
I suggest you start with Java Keywords (Part I) before proceeding further, if you have not read any of the previous articles in the Java Keyword series. Also, go back and read the one about Data Types. All of these articles are from September 2018. That should help you find them quickly. You can also use the "search" option at the top of this page. The series was written with natural progression in mind. Therefore, some of the keywords already covered may be used in code examples illustrated here.
Java keyword list
abstract | continue | for | new | switch |
assert | default | goto* | package | synchronized |
boolean | do | if | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient |
catch | extends | int | short | try |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const* | float | native | super | while |
The throw Keyword
Using the same code example from the previous article, we can add the use of throw keyword to make the code more complete:
public void writeEntry (String filePath, String entry) {
try {
FileWriter fileWriter = new FileWriter(filePath, true);
fileWriter.append("/n");
fileWriter.append(entry);
fileWriter.close();
} catch (IOException ioe) {
// Log the error
throw ioe;
}
// Do more stuff...
}
public Sting doSomethingAndReturnString () {
String myString = null;
//Do something to set a value on myString....
if (myString == null) {
throw new IllegalArgumentException("myString cannot be null");
}
return myString;
}
The throws Keyword
public void writeEntry (String filePath, String entry) throws IOException {
try {
FileWriter fileWriter = new FileWriter(filePath, true);
fileWriter.append("/n");
fileWriter.append(entry);
fileWriter.close();
} catch (IOException ioe) {
// Log the error
throw ioe;
}
// Do more stuff...
}
Order of Execution
It is important that we cover the order of execution of instructions inside a "try/catch/finally" block. Let's use the following example to illustrate:
public void writeEntry (String filePath, String entry) {
System.out.println("Before the try/catch/finally");
try {
System.out.println("Inside the try(1)");
System.out.println("Inside the try(2)"); // Assume exception occurs here
} catch (IOException ioe) {
System.out.println("Inside the catch");
throw ioe;
} finally {
System.out.println("Inside the finally");
}
System.out.println("After the try/catch/finally");
}
Before the try/catch/finally
Inside the try(1)
Inside the catch
Inside the finally
Before the try/catch/finally
Inside the try(1)
Inside the try(2)
Inside the finally
After the try/catch/finally
Closing Words
In parts X and XI of these series, you have learned the correct usage of the 5 keywords necessary to implement all aspects of Exception Handling in the Java language. You should learn these keywords and their application in Exception Handling as this is one of those questions that always seem to be asked in Java programming job interviews. In fact, keywords and their usage is normally asked in entry-level interviews.Next up, Part XII: Implementing Interfaces and Extending Classes
Comments
Post a Comment