Java Masterclass: Java Exceptions, Assertions and Logging

Sam Alapati

Buy Now UK     Buy Now USA

Is your Java system setup to handle programming errors, or exceptions, gracefully and with minimal impact on the user experience? Do you know how to setup assertions to check your code? Do you know how to log events so that issues can be assessed and debugged painlessly and easily? Help is at hand! Welcome to your 'Java Masterclass: Java Exceptions, Assertions and Logging' - a concise, focused book designed to get you up-and-running in this critical world of Java development and administration.

Java Masterclass Book Cover

About This Java Masterclass book

Written by leading Java Expert, Sam Alapati, and aimed at newcomers to the world of exceptions, assertions and logging, this book cuts-to-the-chase in a logical and easy-to-understand manner. Get under the hood and find out about declaring, throwing and catching exceptions; what checked and unchecked exceptions mean; how to define custom exception classes; why (and how) you should use assertions; the value and power of logging; and much more! Even more experienced Java developers and administrators should gain valuable information and knowledge from this book.

> Learn about the different Types of Exception, and how to deal with them elegantly

> Dig deep into Exception Objects and learn how to Define Custom Exceptions

> Discover practical guidelines on the effective use of Exceptions, including when to chain Exceptions

> Sanity check your application code, using simple pass/fail Assertion tests

> Get to grips with java.util.logging, Log4J, and other logging frameworks

> Filled with lots of sample code, hints, tips and notes. Download the code from this website for free.

Who this book is for

As a beginners guide, this book is primarily aimed at newcomers to the world of Java Exceptions, Assertions and Logging. More experienced administrators and developers may also find much of the information valuable and useful.


About The Author

Sam Alapati is Senior Database Architect and Manager at Cash America, and the author of more than a dozen acclaimed IT books. He is an experienced Oracle database administrator who holds the Oracle Certified Professional designation and the Hewlett-Packard UNIX System Administrator certification.

Buy Now UK     Buy Now USA

Book and eBook Published: 10 October 2013 | ISBN: 978-0-9574105-4-1 | 162 Pages | $16.99, £10.99

Sample Content: Guidelines for Using Exceptions in Java 

Table of Contents

Chapter 1 - Handling Java Exceptions
An Overview of Java Exception Handling
The Java Exception Handling Model
Declare the Exception
Throw the Exception
Catch the Exception
Chapter 2 - Types of Exceptions you may Encounter
Unchecked and Checked Exceptions
The Error Subclass of Throwable
The Exception Subclass of Throwable
Different types of Exceptions
Why you must Catch Exceptions
What happens when you don't Catch Exceptions
Handling Exceptions Gracefully
Chapter 3 - Dealing with Java Exceptions
Handling Exceptions When they Occur
The try Block
Nesting a try Block
The catch Block
Specifying multiple catch blocks
Handling multiple exceptions in a single catch block
The Exception Hierarchy and the Syntax of a Good try Statement
The finally Block
Deallocating Resources with the try-with-resources construct
An Example that uses the try/catch Constructs
Declaring Exceptions
The throws Clause
Coding the try Block
Catching the Exceptions
Exception Handling in our Program
Sequence of Program Control
Exceptions in Overridden Methods
Stack Unwinding
Rethrowing an Exception
Passing the Exceptions Instead of Dealing with them
More on the Use of the throws Construct
The Throw Statement and its Mechanics
When to Throw an Exception
  Rethrowing Exceptions
To Throw or to Catch
Chapter 4 - Understanding Exception Objects
The catch-or-declare Requirement
The Throwable Class in Detail
Chapter 5 - Defining Custom Exceptions
Reasons for Creating Custom Exception Classes
Defining your own Exception Classes
Creating a Custom Exception Class
Throwing the Custom Exception
Catching the Custom Exception
A Good Rationale for Creating Custom Exceptions
A More Formal Custom Exception Class example
Guidelines for Creating Custom Exceptions
Chained Exceptions
Exception Chaining and Custom Exception Classes
Chapter 6 - Guidelines for Effective Use of Exceptions
When to use Exceptions
When you neither catch nor throw exceptions
Begin Early
Don't Ever Ignore an Exception
Don't use Exception Handling for every Statement
Maintain Failure Atomicity
Design Immutable Objects
Check Parameters for Validity before the Operation
Attempt to Execute the Code before you Modify the Object
Intercept the Failure
Perform Operations on a Temporary Copy of the Object
Collect Failure Details
Use Standard Java Exceptions
Document All of a Method's Exceptions
Chapter 7 - Using Assertions to Test Your Code
Preconditions and Postconditions
Why Use Assertions
The Syntax of the assert Statement
Enabling and Disabling Assertions
Coding an Assert Statement
Caution when using Assertions
Chapter 8 - Java Logging
Using System.out.println for logging
The Java Logging API
How it Works
The java.util.logging Package
Logging Names
Using the Java Logging API
The Log Manager
Configuring Java Logging
Using a Configuration File
Global Properties
Handler Specific Properties
Facility Specific Properties
Logging Levels
How to Use the Java API for Logging
A Couple of Logging Examples
Defining Custom Loggers
The Logger Hierarchy
What a Log Record Contains
The FileHandler
Filtering and Formatting
Head and Tail
Installing the Formatter into the Handler
Summary of java.util.logging.Logger 1.4
java.util.logging.ConsoleHandler 1.4
java.util.logging.FileHandler 1.4
java.util.logging.LogRecord 1.4
java.util.logging.Filter 1.4
java.util.logging.Formatter 1.4
Summary of the Java Logging API
Chapter 9 - Using Log4J and other Frameworks for Logging
The Log4J Architecture
Working with Loggers
Obtaining a Logger Instance
Configuring the Logger
Configuring Log4J
Initialization Sequence
Configuring with a Properties File
Configuring with an XML File
A Brief Introduction to SLF4J
Native implementation
Logging Best Practices
Don't Log Too Much Information
Output Specific Logging Statements
Log Decision making Statements
Don't Log Sensitive Information