Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Ivor Horton's Beginning Java, Java 7 Edition
|
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2011, 11:29 PM
Registered User
 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Default constructor for your own exceptions and the exception message

Hello!
In chapter 7 on page 300 near the bottom, in regards to instantiating an object of your own exception type (derived from "Exception") by calling its empty default constructor, it says:
Quote:
The message in the exception object consists only of the qualified name of the exception class.
So, if I do the following:
Code:
MyException e = new MyException();
,,,and the default constructor for "MyException" is just empty, I would expect "e" to have a message that consists of the qualified name of the exception class. However, in my code below, the message is null. Am I missing something?

Code:
public class MyException extends Exception {

	public MyException() { } // Empty default constructor.
	
	public MyException(String message) {
		super(message);
		// ...
	}

}

public class TryThis {

	public static void main(String[] args) {
		try {
			throw new MyException(); // Call the default constructor.
		} catch (MyException e) {
			System.err.println(e.getMessage()); // Prints "null"!
		}
	}

}
 
Old May 8th, 2013, 11:58 PM
Registered User
 
Join Date: May 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm just a beginner, but FWIW I agree with you - the text is wrong. The table on Pg 297 is also wrong. getMessage() will ONLY return the actual message that was passed to the exception constructor. The author is confusing getMessage() with the toString() method. The toString() method actually DOES concatenate the class name (appended with a colon) to the message, and if there is no message, it will just print the class name.

So, the text should be corrected, and should describe the behaviour of the two methods getMessage() and toString().

I've done the same test as you, and also checked the Oracle reference documentation for the class Throwable: http://docs.oracle.com/javase/7/docs...Throwable.html

I have just submitted an official errata request.

Regards,
Greg.





Similar Threads
Thread Thread Starter Forum Replies Last Post
TIO page 552: Exception when trying to run Default.aspx in browser mkaftor BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 April 26th, 2011 11:20 AM
Warning Message (Exception ex) dbcook8 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 December 1st, 2008 05:18 PM
Why default constructor may be invoked? enin C# 1 July 26th, 2006 07:26 AM
When to use Exceptions kcraft Pro PHP 1 January 15th, 2005 02:37 PM
Constructor With a Default? n6532l Visual C++ 2 September 18th, 2003 02:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.