Java exceptions: Common terminology with examples [2019 guide]

The Java programming language comes with advanced exception handling features that help programmers manage exceptional events. In Java, exceptions are either thrown by the compiler or occur during the execution of the program. Exceptions stop the normal flow of the program, so you need to handle them appropriately.

The Java programming language comes with advanced exception handling features that help programmers manage exceptional events. In Java, exceptions are either thrown by the compiler or occur during the execution of the program. Exceptions stop the normal flow of the program, so you need to handle them appropriately.

In this article, we’ll have a look at all the terms and examples you need to know about Java exceptions in 2018.

What are Java exceptions?

Java exceptions are events that disrupt the normal execution of the program. The main goal of exceptions is to separate error-handling from regular code. Exceptions might stem from a wide range of problems such as missing resources, coding errors, memory errors, and others. In Java, there are two kinds of exceptions:

  1. Unchecked exceptions (runtime exceptions)
  2. Checked exceptions (compile-time exceptions)

1. Unchecked exceptions

Unchecked exceptions are issues that occur at runtime. They are also called uncaught or runtime exceptions. As unchecked exceptions aren’t checked at compile time, you aren’t required to specify or handle the exception (although you can if you want). They are usually the results of bad coding practices or logical errors such as data errors, invalid arguments, or missing resources.

Basic arithmetic errors such as divide by zero are the easiest example of unchecked exceptions. For instance, here’s a code example that throws [ArithmeticException](https://docs.oracle.com/javase/9/docs/api/java/lang/ArithmeticException.html "ArithmeticException") at runtime:

public class ArithmeticException {

	public static void main(String[] args) {

	    int a = 0;
	    int b = 100;
	    int c = b/a;

	    System.out.println("Result: " + c);
	}

}

Of course, divide by zero is an operation that’s impossible to execute. The console output is:

Exception in thread "main" java.lang.ArithmeticException: / by zero
at arithmetic.ArithmeticException.main(ArithmeticException.java:9)

Wanting to access non-existent data is another typical example of unchecked exceptions. Say, you have an array of six elements and try to print out the item at index 10:

public class InvalidArrayIndex {

	public static void main(String[] args) {
		int myArray[] = {0, 1, 2, 3, 4, 5};

		System.out.println(myArray[10]);

	}

}

As there’s nothing at index 10, the above code example throws [ArrayIndexOutOfBoundsException](https://docs.oracle.com/javase/9/docs/api/java/lang/ArrayIndexOutOfBoundsException.html "ArrayIndexOutOfBoundsException"):

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
	at invalidarrayindex.InvalidArrayIndex.main(InvalidArrayIndex.java:8)

There are more complicated examples of unchecked exceptions as well. The most frequent unchecked exception is called [NullPointerException](https://docs.oracle.com/javase/9/docs/api/java/lang/NullPointerException.html "NullPointerException"). It’s also among the top 4 Java exceptionsRaygun users encounter. NullPointerException happens when your app tries to use null where an object is required. This can be caused by different reasons such as accessing an uninitialized variable or calling the instance method of a null object.

2. Checked exceptions

Checked exceptions are also called compile-time exceptions, as they arise at compile time. Java code containing any checked exceptions won’t compile. When you try to run such code, the compiler warns you about the presence of the checked exception. If you still choose to compile the code you’ll encounter the “unresolved compilation problems” message.

[IOException](https://docs.oracle.com/javase/9/docs/api/java/io/IOException.html "IOException") is one of the most common checked exceptions in Java. It’s caused by different input-output problems such as invalid file access or networking errors.

Here’s an example. The following code intends to access a non-existent file, then write something into it:

import java.io.FileWriter;

public class FileNotFound {

   public static void main(String args[]) {		
      FileWriter myWriter = new FileWriter("C://filenotfound.txt");

      myWriter.write("Hi, I'm trying to write something.");
      myWriter.close();
   }
}

Needless to say, it’s not possible to write anything into a file that doesn’t exist. My Eclipse IDE informs me about the presence of IOException and asks if I still want to compile the code.

Remember that the compiler had no complaints in the case of unchecked exceptions, as they will be thrown only at runtime.

If I proceed with the unhandled exception the compiler returns the following error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
	Unhandled exception type IOException
	Unhandled exception type IOException
	Unhandled exception type IOException

	at fileNotFound.FileNotFound.main(FileNotFound.java:8)

To solve the compilation problem, the method in which the checked exception occurs needs to either handle or at least specify it. Before showing how to do that, let’s have a look at Java’s Exception class hierarchy.

3. Exception classes

Exception classes are Java classes that include all checked and unchecked exceptions. The exceptions I mentioned before such as ArithmeticException and IOException are all represented by an exception class. In fact, Java has an extensive hierarchy of parent and child classes that cover most issues that may occur during compilation or at runtime.

The classification of Java exceptions is a bit confusing, however. The [Throwable](https://docs.oracle.com/javase/9/docs/api/java/lang/Throwable.html "Throwable") class is the superclass of all Java exceptions and errors. It has two subclasses, [Error](https://docs.oracle.com/javase/9/docs/api/java/lang/Error.html "Error") and [Exception](https://docs.oracle.com/javase/9/docs/api/java/lang/Exception.html "Exception") but they don’t represent checked and unchecked exceptions. The Exception class has a subclass called [RuntimeException](https://docs.oracle.com/javase/9/docs/api/java/lang/RuntimeException.html "RuntimeException") that contains most unchecked exceptions. All other subclasses of Exceptionare handled as checked exceptions. Besides runtime exceptions, errors also count as unchecked exceptions.

So: - Subclasses of Exception except for the subclasses of RuntimeException are regarded as checked exceptions. - Subclasses of Error and RuntimeException are regarded as unchecked exceptions.

ErrorException, and RuntimeException all have several subclasses. For example, IOException is a subclass of Exception and NullPointerException is a subclass of RuntimeException.

You may have noticed that Java differentiates errors from exceptions.

Why is that so?

In Java, errors indicate abnormal conditions such as stack overflow that applications shouldn’t try to catch. They are mostly caused by the environment, as opposed to exceptions that are caused by the program itself.

Exceptions are caught either at compile time or at runtime. Errors terminate the program for sure and they cannot be handled in any way. However, exceptions can be recovered by certain programming techniques.

4. Handling exceptions

Exception handling in Java happens with the trycatch, and finally blocks. You can use them to define how you want to handle exceptions when they occur. The try block should include the code that may or may not throw an exception. Each catch block is an exception handler that deals with an exception thrown by the try block. The finally block executes in any case, whether an exception has been thrown or not.

You can include as many catch blocks as you want. However, you can add only one finally to each try block. Programmers usually use finally blocks to do the cleanup after the try and catch blocks have been executed.

Here’s the general syntax of Java’s exception handling:

try {

   // Code that might throw exceptions.

} catch (Exception e1) {


   // Catch block 1. Executes if try block throws e1.


} catch (Exception e2) {


   // Catch block 2. Executes if try block throws e2.


} catch (Exception e3) {


   // Catch block 3. Executes if try block throws e3.


} finally {


   // Cleanup code. Always executes.


}

Handling checked exceptions

Here’s how you can handle the IOException example we have discussed above, using a try-catchblock. We enclose the sensitive code that might throw an IOException with a try block. The catchblock only fires if the try block throws an IOException.

public class FileNotFound {

   public static void main(String args[]) {	    
FileWriter myWriter;

		try {

			myWriter = new FileWriter("C://filenotfound.txt");

			myWriter.write("Hi, I'm trying to write something.");
		    myWriter.close();

		} catch (IOException e) {

			System.out.println("Exception thrown: " + e);

		} finally {

			System.out.println("End of execution.");

		}

   }

}

Now, the console output changes from the system-generated message to the instructions we’ve given inside the catch block. The code throws [FileNotFoundException](https://docs.oracle.com/javase/9/docs/api/java/io/FileNotFoundException.html "FileNotFoundException") instead of IOException, which is a subclass of IOException and provides the closest description of the issue.

Exception thrown: java.io.FileNotFoundException: C:\filenotfound.txt (Access is denied)
End of execution.

Handling unchecked exceptions

You can also use try-catch blocks to handle an unchecked exception. Although handling unchecked exceptions is not obligatory, it’s a good idea to use a try-catch block for logging and monitoring purposes. The following example is from the Java Programming Wikibook and it demonstrates how logging an unchecked exception can be useful in servlet programming:

public long getLastModified(HttpServletRequest req) {


 try {


   ...


   return getTimeStamp();


   ...


 } catch(RuntimeException e) {


   log.error("Error during handling post request", e);


   throw e;


 }


}

The above code is called when an application server makes a request to the server. First, it catches runtime exceptions for logging purposes. Then, it throws them back to the server so that it can handle them. This way the application has its own logging system separate from the server logging and can detect all runtime exceptions on its own.

Throwing exceptions

Throwing exceptions is a specific programming technique in Java. You can only catch an exception that was thrown before, either by the Java platform or custom code. Exception throwing in Java happens with the [throw](https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html "throw") statementArithmeticExceptionArrayIndexOutOfBoundsExceptionNullPointerException, and IOException in our examples were all thrown automatically by the Java platform.

The throw statement

The throw statement can be used with any throwable object that inherits from the Throwable class. It explicitly throws an exception from any block of code. Each throw statement inside a try block needs to be handled by a corresponding catch block.

For instance, our IOException example could also be written this way:

public class FileNotFound {

   public static void main(String args[]) {	    

	   FileWriter myWriter;

		try {

			myWriter = new FileWriter("C://filenotfound.txt");

			myWriter.write("Hi, I'm trying to write something.");
			myWriter.close();

			throw new IOException();

		} catch (IOException e) {

			System.out.println("Exception thrown: " + e);

		} finally {

			System.out.println("End of execution.");

		}

   }

}

You can add as many throw statements to a try block as you want, however, each has to have its own corresponding catch block.

Besides, catch blocks can also include their own throw statement. You could have seen that in the HttpServletRequest example before. When a catch statement throws an exception it needs to be further handled by the code calling the method. This technique is also referred to as rethrowing an exception.

The throws statement

Besides throw, Java also has a throws statement. It can be used in the signature of any method that might throw a checked exception. Say, you have a checked exception that would prevent the code from compiling but you don’t want to or can’t handle it within the current method. Using the throws keyword, you can indicate that the caller has to handle this exception with its own try-catch block.

This is how our IOException example looks like when we don’t handle it directly but delegate it to the caller method:

public class FileNotFound {

public static void main(String args[]) throws IOException {	    	    

	FileWriter myWriter;

	myWriter = new FileWriter("C://filenotfound.txt");

	myWriter.write("Hi, I'm trying to write something.");
	myWriter.close();		

}

}

As you can see, there’s no handling with a try-catch block. We just indicate the presence of the exception in the method signature so that the caller can decide how it wants to handle it.

You can add as many exceptions after the throws keyword in the method signature as you want. For example:

public static void main(String args[]) throws IOException, IllegalAccessException

Grouping exceptions

Grouping exceptions is a programming shortcut that allows you to handle multiple exceptions in a single catch block. This technique is available since Java 7. It’s a great solution when you want to run the same code on different exceptions. As you don’t have to handle each exception in a separate catch clause, you will have a much cleaner code base.

For example, you can handle IOException and IllegalAccessException in the same catch block:

catch (IOException | IllegalAccessException e) {

System.out.println("Exception thrown: " + e);

}

There’s one important rule, however. You can only group exceptions that are unrelated to each other. It’s not allowed to group exceptions that have a parent-child relationship. For example, the following code won’t compile, as FileNotFoundException is a subclass of IOException:

catch (IOException | FileNotFoundException e) {

System.out.println("Exception thrown: " + e);

}

The compiler returns an “unresolved compilation problem” message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
	The exception FileNotFoundException is already caught by the alternative IOException

	at fileNotFound.FileNotFound.main(FileNotFound.java:22)

Nesting exceptions

Exception nesting happens in Java when one exception results in a second exception which results in a third exception, etc. As you deal with a whole chain of exceptions, nested exceptions are also called chained exceptions. The stack trace of an exception contains the chain of exceptions that have lead to the final exception.

You can nest a Java exception into another one using of the catch clause. Nested exceptions can help you identify why an exception has been thrown.

For instance, this is how exception nesting works with our IOException example:

public class FileNotFound {

   public static void main(String args[]) throws Exception {	    

	   FileWriter myWriter;

	    try {

			myWriter = new FileWriter("C://filenotfound.txt");

			myWriter.write("Hi, I'm trying to write something.");
		    myWriter.close();

		} catch (IOException e1) {

			Exception e2 = new Exception("Couldn't write into a non-existent file.", e1);

			throw e2;

		}

   }

}

As you can see the catch block creates a second exception called e2 that specifies the reason of e2and refers e1 as the cause of the exception. Then, it rethrows the new exception so that the caller method can handle it (or rethrow it again).

The Exception class has five possible constructors; the above example uses the following one:

public Exception(String message, Throwable cause)

If you run the example code, the console prints the whole stack trace:Exception in thread "main" java.lang.Exception: Couldn't write into non-existent file.
	at fileNotFound.FileNotFound.main(FileNotFound.java:21)
Caused by: java.io.FileNotFoundException: C:\filenotfound.txt (Access is denied)
	at java.base/java.io.FileOutputStream.open0(Native Method)
	at java.base/java.io.FileOutputStream.open(Unknown Source)
	at java.base/java.io.FileOutputStream.<init>(Unknown Source)
	at java.base/java.io.FileOutputStream.<init>(Unknown Source)
	at java.base/java.io.FileWriter.<init>(Unknown Source)
	at fileNotFound.FileNotFound.main(FileNotFound.java:14)

The stack trace starts with the custom message we defined as the first argument of e2. This shows that the new exception has been nested properly. The e2 exception also keeps a reference to e1, its nesting exception.

Note that we added e2 to the method signature using the throws keyword. So, the code that calls the main() method will have to handle e2 with a try-catch block. This kind of exception conversion can happen as long as a caller method is willing to handle it, instead of just re-throwing it to the next caller.

Need help discovering Java exceptions?

Java exceptions can be difficult to discover in the first place. Raygun Crash Reporting surfaces the stack trace where the error occurred so you can resolve issues quickly. Target Java errors with Raygun.

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Description
This is the best course to learn to program in Java in Spanish from scratch and without any experience in this fabulous programming language. This is the first course where we will study the Fundamentals of Java, and we will take you step by step until you acquire the bases of the Java language and you can start to study more advanced Java topics.

The content is divided into perfectly structured levels, each level supported by the previous one, with the aim of adding Java knowledge incrementally and so you can focus on mastering the issues little by little and gradually. So ensure the success of your Java training.

We will also offer support for any doubts about the didactic material included in this Java Fundamentals course.

We manage a new teaching methodology that we have called Speed ​​Learning. This methodology consists of concise videos that go directly to the point to be studied, complemented by eBooks with explanations and step-by-step images (which you can print, or search for any text you need, or use for your offline study), since As we know we can not do text search within a video. In addition, our methodology includes perfectly structured and very didactic exercises that will allow you to accelerate your eLearning learning. No loss of time in videos where you have to watch the instructor codify an exercise, too much theory, little practice or anything like that. Our Speed ​​Learning methodology guarantees that in the shortest possible time you will acquire the necessary knowledge for the Java professional and working world.

The Java Fundamentals course includes the following topics for study:

Lesson 1 - Starting with Java Technology

The amazing world of Java programming

What is Java technology (from a practical approach)

Our first Java program from scratch

Lesson 2 - Variables and Operators in Java

Use of Variables in Java and what we use them for

Types of Data in Java and how they are classified

Management and Classification of operators in Java

Lesson 3 - Control statements in Java

Using the if-else structure and where to use it

Handling the switch structure and when applying it

Lesson 4 - Handling Loops in Java

Use of the for loop and its use

Using the while loop and how to apply it

Use of the do-while loop and when to use it

Lesson 5 - Object Oriented Programming

Introduction to Object Oriented Programming (OOP)

Handling Classes in Java

Use of Objects in Java

Lesson 6 - Functions in Java

Declaration of Methods or Functions in Java

Use and call of functions in Java

Lesson 7 - Data Management in Java

Using Arrays in Java

Management of Matrices in Java

Lesson 8 - Inheritance in Java

Inheritance Management in Java

Use of superclasses and subclasses in Java

Final Level Laboratory

Final Exercise where everything learned in this Level is integrated

At the end you get a certificate of having completed the Java Fundamentals course.

We wait for you on the other side.

Ing. Ubaldo Acosta

Founder of Global Mentoring

Passion for Java Technology

Who this course is for:

Anyone who wants to learn how to program in Java
Basic knowledge
Basic knowledge of PC use
Basic management of an operating system such as Windows, Mac or Linux
It is not necessary to know how to program, we will start from scratch !!!
The attitude and desire to start coding and learning Java once and for all from scratch!
What will you learn
Have the basics of the programming language with Java
You will know the basic syntax of the Java language
Manage the concept of Variables and Operators in Java
We will study Object Oriented Programming with Java
You will learn the Control Statements and Loops in Java
We will see the concept of Functions with Java
We will study the concept of Inheritance in Java
We will learn to use Arrays in java
We will handle the concept of Matrices in Java
We will learn to Design Classes in Java
We will make a final application with everything learned in the course
To know more:

Java Essentials : Learn Core Java From Basic to Advance

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

Description
This is only Java related course and it's great because it covers just the right amount of Java which is needed to leaning programming, java.

This is a comprehensive yet simple course on java programming language and it concentrates on Java programming concepts.

*************************** No Prior Coding Experience Needed ***************************

This course assumes that you have no programming background. If you have some experience then, it's just a bonus point. You have never code, have some experience or have a lot of experience any other programming language, this course is one stop place for you.

Java is one of the most and useful programming languages to learn You can build back-end of web applications and build robust test automation framework. Specially for Selenium WebDriver GUI automation, Java is most popular choice and has the largest community.

Each lecture consist of a video screencast and code files

There are quizzes, homework to test your knowledge

High focus on practice and asking questions

You will also learn coding best practices

Market is never short of jobs in Java programming language, there are ample of jobs in both Java development and Automation Testing using Java.

What are you waiting for? Enroll today and learn the powerful Java language !!!

Basic knowledge
Nothing else! It’s just you, your computer and your hunger to get started today
Java concepts are covered in the course, no experience needed
Windows/MAC computer
What will you learn
You will be able to EXPLAIN, DESIGN and IMPLEMENT efficient java Programs
You will be confident to clear test automation interviews
Understand the concepts of Object Oriented Programming Language
Complete understanding of java
Expert-level knowledge of Java code (+ advanced tips and tricks used by the pros)
Suitable for beginner programmers and ideal for users who learn faster when shown
To learn more:

Fundamentos de Java: Aprende Java desde cero, sin misterios | Simpliv

Fundamentos de Java: Aprende Java desde cero, sin misterios | Simpliv

Fundamentos de Java: Aprende Java desde cero, sin misterios

Description
This is the best course to learn to program in Java in Spanish from scratch and without any experience in this fabulous programming language . This is the first course where we will study the Java Fundamentals, and we will take you step by step until you acquire the basics of the Java language and so you can start studying more advanced Java topics.

The content is divided into perfectly structured levels , each level supported by the previous one, with the aim of adding Java knowledge incrementally so that you can focus on mastering the issues little by little and gradually. So ensure the success of your Java training.

In other offer support of any doubt teaching materials included in this course Fundamentals of Java.

To make matters worse, we handle a new teaching methodology that we have called Speed ​​Learning. This methodology consists of concise videos that go directly to the point to study, complemented with eBooks with explanations and step-by-step images (which you can print, or search for any text you need, or use for your offline study), since as we know we cannot do text search within a video. In addition, our methodology includes perfectly structured and very didactic exercises, which will allow you to accelerate your eLearning learning. Without wasting time on videos where you have to watch the instructor codify an exercise, too much theory, little practice or anything like that. Our Speed ​​Learning methodology guarantees that in the shortest possible time you will acquire the necessary knowledge for the professional and professional world of Java.

The Java Fundamentals course includes the following topics of study:

Level. Java basics

Lesson 1 - Starting with Java Technology

The amazing world of Java programming
What is Java technology (from a practical approach)
Our first Java program from scratch
Lesson 2 - Variables and Operators in Java

Use of Variables in Java and what we use them for
Data types in Java and how they are classified
Operator Management and Classification in Java
Lesson 3 - Control sentences in Java

Use of the if-else structure and where to use it
Management of the switch structure and when to apply it
Lesson 4 - Cycle Management in Java

Use of the for cycle and its use
Use of the while cycle and how to apply it
Use of the do-while cycle and when to use it
Lesson 5 - Object Oriented Programming

Introduction to Object Oriented Programming (OOP)
Class Management in Java
Using Objects in Java
Lesson 6 - Functions in Java

Declaration of Methods or Functions in Java
Use and call of functions in Java
Lesson 7 - Data Management in Java

Using Arrangements in Java
Matrix Management in Java
Lesson 8 - Inheritance in Java

Inheritance Management in Java
Use of superclasses and subclasses in Java
Final Level Laboratory

Final Exercise where everything learned in this Level is integrated
At the end you get a certificate of having completed the Java Fundamentals course.

We wait for you from the other side.

Ing. Ubaldo Acosta

Founder of Global Mentoring

Passion for Java Technology

Who this course is for:

Anyone who wants to learn to program in Java
Basic knowledge
Basic knowledge of PC use
Basic operation of an operating system such as Windows, Mac or Linux
It is not required to know how to program, we will start from scratch !!!
The attitude and desire to start coding and learning Java once and for all from scratch !!!
What will you learn
Have the basics of the programming language with Java
You will know the basic syntax of the Java language
Will handle the concept of Variables and Operators in Java
We will study Object Oriented Programming with Java
You will learn Control Sentences and Cycles in Java
We will see the concept of Functions with Java
We will study the concept of Inheritance in Java
We will learn to use Arrangements in java
We will handle the concept of Matrices in Java
We will learn to Design Classes in Java
We will make a final application with everything learned in the course
To continue: