March 2018
Intermediate to advanced
208 pages
4h 52m
English
| | class TransmissionParser { |
| | static Transmission parse(String rawMessage) { |
| | if (rawMessage != null |
| | && rawMessage.length() != Transmission.MESSAGE_LENGTH) { |
| » | throw new IllegalArgumentException(); |
| | } |
| | |
| | String rawId = rawMessage.substring(0, Transmission.ID_LENGTH); |
| | String rawContent = rawMessage.substring(Transmission.ID_LENGTH); |
| | try { |
| | int id = Integer.parseInt(rawId); |
| | String content = rawContent.trim(); |
| | return new Transmission(id, content); |
| | } catch (NumberFormatException e) { |
| » | throw new IllegalArgumentException("Bad message received!"); |
| | } |
| | } |
| | } |
Exception handling is not only about catching exceptions, but also about throwing them. When throwing an exception, you should ...