Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Java Workshop

You're reading from   The Java Workshop Learn object-oriented programming and kickstart your career in software development

Arrow left icon
Product type Paperback
Published in Oct 2019
Publisher Packt
ISBN-13 9781838986698
Length 606 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Eric Foster-Johnson Eric Foster-Johnson
Author Profile Icon Eric Foster-Johnson
Eric Foster-Johnson
Andreas Göransson Andreas Göransson
Author Profile Icon Andreas Göransson
Andreas Göransson
David Cuartielles David Cuartielles
Author Profile Icon David Cuartielles
David Cuartielles
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Getting Started 2. Learning the Basics FREE CHAPTER 3. Object-Oriented Programming 4. Collections, Lists and Java's Built-In APIs 5. Exceptions 6. Libraries, Packages, and Modules 7. Databases and JDBC 8. Sockets, Files, and Streams 9. Working with HTTP 10. Encryption 11. Processes 12. Regular Expressions 13. Functional Programming with Lambda Expressions 14. Recursion 15. Processing Data with Streams 16. Predicates and Other Functional Interfaces 17. Reactive Programming with Java Flow 18. Unit Testing Appendix

5. Exceptions

Activity 1: Designing an Exception Class Logging Data

Solution

  1. Import the relevant classes needed for this program to work: NoSuchFileException and logging.
    import java.nio.file.NoSuchFileException;
    import java.util.logging.*;
  2. Make your own methods for issuing exceptions, start by creating one for the NullPointerException case.
    public static void issuePointerException() throws NullPointerException {
        throw new NullPointerException("Exception: null pointer");
    }
  3. You will also need a method for the case the file is not found: NoSuchFileException.
    public static void issueFileException() throws NoSuchFileException {
        throw new NoSuchFileException("Exception: file not found");
    }
  4. Back to the Main method, create the logger object that will be reporting the exceptions and their severity level.
    Logger logger = Logger.getAnonymousLogger();
  5. Capture the argument passed to the script via the CLI...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime