CSCE145 F2026 08 Exceptions
Overview
Java exceptions are objects that signal runtime problems through messages; the lesson explains how to create custom exception classes, declare and throw exceptions, and handle them with ordered catch blocks and optional finally blocks. A console calculator applies these techniques with custom DivideByZeroException and UnknownOpException classes, a precision-based near-zero check, and recovery that keeps the program running after invalid operations.
Key takeaways
- A custom Java exception extends a predefined exception class, and its constructor should pass a useful diagnostic message to super.
- throws documents possible exceptions in a method signature, while throw new ExceptionType(...) raises an exception at the point where an error condition occurs.
- Catch subclasses before superclasses; placing a broad Exception catch first can make specific catches unreachable and cause a compile-time error.
- Floating-point division guards can compare a denominator against positive and negative precision thresholds instead of relying on exact equality with 0.0.
- The calculator recovers from divide-by-zero and unknown-operator errors by catching them around evaluate, allowing later operations to continue.
- A general Exception catch can also handle unexpected input failures such as Scanner's InputMismatchException, while finally can perform cleanup and display the current result.
Chapters
- Exceptions are Java objects with messages describing runtime problems; creating, throwing, and handling them are the three main tasks.
- A custom exception extends a predefined class such as Exception and defines constructors that call super with an informative message.
- The DivideByZeroException example provides default and parameterized constructors, including a message such as “attempted to divide by zero.”
- Place throws followed by exception class names in a method signature to notify callers of possible exceptions; separate multiple types with commas.
- Use throw inside the method body, followed by new and an exception constructor, to create and raise an exception when its error condition occurs.
- The evaluate method declares that it may throw DivideByZeroException and UnknownOpException.
- Put calls that may throw exceptions inside a try block and declare the exception type and an identifier in each catch block.
- Order catches from specific to general: catch DivideByZeroException before its superclass Exception to avoid unreachable-code errors.
- Use getMessage or printStackTrace to inspect an exception; printStackTrace also reports the call stack.
- An optional finally block runs whether or not an exception occurs, and execution continues after the try-catch sequence.
- The calculator tracks a running result and reads console input in operator-space-number format, such as “+ 3”; entering E exits.
- A Scanner reads the operator with next() and the operand with nextDouble(), while evaluate uses a switch for +, -, *, and /.
- Division checks whether the denominator lies between negative and positive precision thresholds, treating sufficiently small double values as zero.
- DivideByZeroException extends Exception and passes a division-by-zero message to the superclass constructor.
- UnknownOpException also extends Exception and offers constructors for a default message or a message identifying the unrecognized operator.
- The evaluate method throws DivideByZeroException for a near-zero denominator and UnknownOpException from the switch default case.
- runCalculator wraps evaluate in try-catch blocks for DivideByZeroException and UnknownOpException, followed by a general Exception catch.
- A finally block calls keyboard.nextLine() to clear Scanner input after nextDouble(), then prints the current result.
- Tests show division by zero and an unsupported “mod” operator report stack traces without ending the calculator; a missing numeric operand triggers InputMismatchException, which the general catch handles.
Transcript excerpt
0:00 Hey everyone, how goes it? And welcome back to computer science 145. Today we're going to discuss exceptions. Exceptions signal an exceptional occurrence during runtime. Now these handle runtime errors by allowing the program to crash gracefully and keep executing. In Java, exceptions are objects and these objects have an exception message. So in other words, what went wrong during runtime? We use the term throwing whenever an exception object is created. We also use the term handling when special code detects and deals with that exceptional occurrence. There's three major parts to exceptions. There's creating exceptions, throwing,
0:48 in other words, using exceptions, and also handling exceptions. So, let's take a look at each one of these parts. In Java, there are several predefined exceptions like the first one is exception and that's the most general exception. You also have null pointer exception, index out-of-bounds exception, io exceptions and some of these you may have encountered before. Now when creating exceptions, what we have to do is inherit from one of the predefined exceptions. As you can see in the syntax over here, we have public class because remember exceptions are considered objects. So public class the identifier then followed by the reserved word extends
1:36 and then an exception. Now when we write our own exceptions, we only write the constructors. As you can see right here, after we have extended a predefined exception, we only write the constructors inside the body of this class. Now when we do write the constructors, it works the same way as writing constructors for any other inherited object. We need to make sure to use the reserved word super in order to construct the super class or in this case the super exception. As mentioned before, exceptions are considered objects and those objects
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.