KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Catching Exceptions

try {
int notANumber = Integer.parseInt("You are not alone!");
} catch (NumberFormatException e) {
System.out.println("That's not a number!");
}

We’ll spend the next few lessons learning about Java exceptions. Errors are a normal part of programming, and Java provides nice ways for handling them. Let’s learn more!

Exceptions: When Things Go Wrong
Exceptions: When Things Go Wrong

It’s natural to make mistakes when you write computer programs. But even well-designed programs may encounter errors!

Imagine the following scenario. You design an app that prompts the user to enter a number that you plan to use in a mathematical calculation. What you receive is a String, so you need to convert it to an Integer. What could go wrong? Let’s find out!

// Problems with Integer Parsing

What is happening here? Let’s examine the documentation for Integer.parseInt to find out.

try-catch
try-catch

In Java, when code that we write or call encounters an error, it can throw an Exception. Over the next few lessons we’ll explore Java’s error-handling mechanisms, including types of exceptions and how to design and throw them in our own code.

But let’s start at looking at how to handle exceptions that we might encounter. To do this we use a new Java programming construct: try-catch. Let’s see how that works!

// try-catch

try-catch consists of two or more code blocks. First, the try block, containing the code that might throw an exception. Second, one or more catch blocks that handle various kinds of exceptions that the code might throw. Let’s look at some code that can generate several kinds of exceptions and see how to handle them:

import java.util.Random
Random random = new Random();
int choice = random.nextInt()

Practice: Catching Exceptions

Created By: Geoffrey Challen
/ Version: 2020.11.0

Create a public class called Catcher that defines a single class method named catcher. catcher takes, as a single parameter, a Faulter that has a fault method. You should call that fault method. If it generates no exception, you should return 0. If it generates a null pointer exception, you should return 1. If it throws an illegal argument exception, you should return 2. If it creates an illegal state exception, you should return 3. If it generates an array index out of bounds exception, you should return 4.

Exception Control Flow
Exception Control Flow

One of the more difficult parts of exceptions is understanding how code flow changes when an exception is thrown. When an exception is thrown, Java jumps into the first enclosing catch block. This might be in that method, or in calling method, or even in the caller’s caller or higher up. Let’s look at an admittedly contrived example:

void foo1() {
foo2();
}
void foo2() {
foo3();
}
void foo3() {
foo4();
}
void foo4() {
Object o = null;
System.out.println(o.hashCode());
}

Don’t worry if this doesn’t make perfect sense yet—we’ll get lots of practice with this over the next few days!

Practice: Multi Catcher

Created By: Geoffrey Challen
/ Version: 2021.9.0

Provide a public class Catcher that implements a static method named retrieveValue. retrieveValue takes an instance of Faulter and returns the result of calling its getValue method, which returns an int.

Seems simple! Except there is just one small problem. Faulter was implemented by a friend that didn't take CS 124, and so it's getter is pretty buggy. A lot of the time it will throw an exception rather than return the value. But you did take CS 124, and so you know how to catch the exception and retry the call to getValue. Note that getValue may fault multiple times before succeeding, and you should retry until it successfully returns a value.

Homework: Guess The Secret Number

Created By: Geoffrey Challen
/ Version: 2021.9.0

Let's play a guessing game! Create a public class name Guesser that provides a single class method named guessValue. guessValue accepts a single Secret instance which provides a guess method that accepts an integer. If the integer is not the secret value, guess will throw an exception. Otherwise, it does not.

guessValue should return the secret value stored by the Secret instance. However, you should only guess values between 0 and 32, inclusive. If you guess values outside that range, the Secret instance will break you will no longer be able to extract the secret! Also note that some Secrets will contain invalid values. If you are presented with a Secret that does not have a secret value between 0 and 32, inclusive, throw an assertion error. (One way to do this is simply assert false.)

More Practice

Need more practice? Head over to the practice page.