14 06 handling of several exception
In this lesson we work inside a Car constructor and add a condition that raises a NotACarException when the value is not valid. The idea is to tell the constructor: if the caller tries to create a Car instance with a negative mileage, build a NotACarException object instead. We use the throw keyword for that, and we also declare it on the constructor with throws NotACarException.
The throws keyword in the signature warns the JVM that this piece of code is potentially dangerous and needs to be invoked from a try/catch block. It is followed by the name of the exception class that callers must handle. The throw keyword, on the other hand, is what actually raises the exception manually by instantiating an exception object.
Catching the custom exception at the call site
- The constructor declares
public Car(...) throws NotACarException. - Inside the constructor we
throw new NotACarException(...)when the input is invalid. - Where we build a new car (for example
new Car("Yaris", "Toyota")) the compiler now requires atry/catchonNotACarException.
Once we wrap the instantiation in a try block followed by a catch (NotACarException e), the compilation error disappears and the code is ready to run. The mileage variable, declared as an int, will not let us pass a textual value by mistake, so the type system already filters out part of the bad inputs and our custom exception covers the rest.
Summary
This lesson introduces exception handling in Java by demonstrating how to create and throw a custom exception (NonVoitureException) in a Car class constructor when invalid data is provided (negative kilometers). It explains the use of the 'throws' keyword in method signatures to declare potential exceptions and the 'throw' keyword to manually trigger exceptions, alongside implementing try-catch blocks to properly handle these exceptions in client code.
Key points
- Creating custom exceptions: define a custom exception class (e.g., NonVoitureException) to handle domain-specific errors
- Using 'throws' keyword: declare in a method signature which exceptions it may throw, informing the JVM and calling code of potential dangers
- Using 'throw' keyword: manually instantiate and throw an exception when validation fails (e.g., negative kilometers input)
- Try-catch blocks: mandatory construct to handle checked exceptions when calling methods that declare 'throws'
- Exception propagation: when a method declares 'throws', calling code must either handle the exception or propagate it further
FAQ
Why do we throw an exception for negative kilometers in the Car constructor?
Throwing an exception enforces data validation at object creation time, preventing the instantiation of a Car with invalid state. This is better than allowing a Car to exist with negative kilometers, which violates the business logic.
What is the difference between 'throws' and 'throw'?
'throws' (in a method signature) declares that a method may throw an exception, warning calling code to handle it. 'throw' (in method body) actually creates and throws an exception instance when a condition is met.
Why must we use try-catch when creating a Car object if the constructor throws an exception?
Since the constructor is declared with 'throws NonVoitureException', Java requires calling code to either handle (catch) the exception or propagate it further. This enforces explicit error handling instead of silently ignoring potential failures.