DEV Community

Cover image for Java Quickies. Exceptions
Tristan Elliott
Tristan Elliott

Posted on • Edited on

Java Quickies. Exceptions

Introduction

  • This series is going to be dedicated to the basic understanding of Java. When ever I find myself asking, "How does this work ?". I will create a blog post and put it here. This series will not be in order so feel free to read what ever post you find most relevant.

What is an Exception?

  • Well, first off an exception is shorthand for the phrase, "exceptional event". A exception is defined as an event which occurs during the execution of a program, that disrupts the normal flow of a program's instructions.

  • When an error occurs within a method, that method creates an object and hands it off to the runtime system. That object is called an exception object and it contains information about the error, including its type and the current state of the program. Creating an exception object and handing it to the runtime system is called throwing an exception.

What is the Java Runtime System

  • In basic software terms, a runtime system is a piece of software that is used to run code. The Java runtime system consists of 2 main parts.

  • 1) Class Loader : this is responsible for correctly loading classes and connecting them with the core Java class libraries.

  • 2) JVM : the Java virtual machine actually does quite a few things but basically it is what allows us to run our Java code.

  • The Java Runtime System is a layer of software that runs on top of a computer's operating system making sure that everything runs properly.

What happens when an exception is thrown?

  • Once a method has thrown an exception the runtime attempts to find a method to handle it. The runtime searches the call stack for a method that contains a block of code to handle the exception. That block of code is called a exception handler.

The Catch or Specify Requirement

  • To write valid exception handling code, we must honor the catch or specify requirement. This means that code dealing with exceptions must follow one of the two definitions listed below

  • 1) A try/catch block must be put into place. More on this later.

  • 2) A method must provide a throws clause, that lists the exception. More on this later

  • When dealing with exceptions we must first identify the exception. Exceptions are actually broken down into 3 categories:

  • 1) Checked exception : these are minor errors and any well crafted program should be able to handle and recover from these. A simple example would be if a user provided the wrong name to a file reader. Our program should be able to handle this situation and return the appropriate response.

  • 2) Error : an exception is deemed an error if the exception thrown is due to an external cause, something outside of our application. Usually our application can not recover from these kind of exceptions. An example would be a server going offline.

  • 3) Runtime exceptions : this kind of exception is created by programming bugs, such as logic errors or the improper use of an API. While a application can recover from these, it makes more sense to just eliminate the bug.

-errors and runtime exceptions are both considered to unchecked errors

The Try Block

  • The first step to creating an exception handler is to create an try block.
try{
  // code goes here 
}
Enter fullscreen mode Exit fullscreen mode
  • Code that might throw an exception should be enclosed inside of the try block. If an exception occurs inside of the try block, the exception is handled by the exception handler that is associated with it. To associate an exception handler with a try block, we must put a catch block directly after it.
try{
  // code goes here 
}catch(ExceptionType name){
 // exception handling code here
}
Enter fullscreen mode Exit fullscreen mode
  • You are allowed to put more than one catch block but understand that each catch block is only an exception handler for the type of exception that we define.
try{
  // code goes here 
}catch(IoException error){
 // exception handling code here
}
Enter fullscreen mode Exit fullscreen mode
  • This catch block will only get called when an IOException occurs and it will ignore all other exceptions. This has been a very simplified version of Java's try/catch block, so I would highly recommend that you read the entire documentation provided to us by Oracle.

Specifying the Exceptions thrown by a Method.

  • If you are following along in my Maven build series please make sure to pay close attention to this upcoming syntax. As it will be used often in the series

  • Sometimes it is appropriate for methods to attach exceptions that can occur within it. This is what is done in try/catch blocks. However, there are times when it is better to let a method further up the call stack handle the exception.

  • If a method does not catch the exception that occurs within it, then the method must specify that it can throw an exception. It does this with the "throws" clause

public void writelist() throws IOException{
  // normal code will go here 
}
Enter fullscreen mode Exit fullscreen mode
  • The throws keyword tells the compiler that there is a method higher up in the call stack that will handle the IOException if one is thrown. This can be handy because it lets us write less code. We only have to write one exception handler instead of many.

Conclusion

  • Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
  • Also make sure to checkout my YouTube channel for more programming tutorials.

Top comments (0)