Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++
| Search | Today's Posts | Mark Forums Read
BOOK: Professional C++
This is the forum to discuss the Wrox book Professional C++ by Nicholas A. Solter, Scott J. Kleper; ISBN: 9780764574849
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C++ 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
  #1 (permalink)  
Old December 4th, 2014, 07:26 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default java does not require exception be caught or declared

In the 2nd edition: some comments about java appear to be incorrect:
e.g.
"This behavior is different from that in other languages, such as Java, which requires a function or method to catch exceptions or declare them in their own function or method throw lists."
That's misleading because the third option, commonly employed in java is to use unchecked exceptions. Unchecked exceptions in java behave the same as C++ exceptions.

Here is an example demonstrating that catching/declaring exceptions is optional:
public class testException {
static Integer myValueOf(String s) throws NumberFormatException {
return Integer.valueOf(s);
}
public static void main(String[] args) {
try {
myValueOf("bad int1");
} catch (NumberFormatException e) {
System.out.println("int1");
}
myValueOf("bad int2");
}
}
Output:
int1
Exception in thread "main" java.lang.NumberFormatException: For input string: "bad int2"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at testException.myValueOf(testException.java:3)
at testException.main(testException.java:11)

The compiler does not require the exception thrown by method call myValueOf("bad int2"); to be caught, nor declare it in the function throw list.

To contrast two languages based on a feature that behaves the same way makes no sense.

Last edited by murray hughes; December 4th, 2014 at 07:28 PM.. Reason: fixed mistake in title


Similar Threads
Thread Thread Starter Forum Replies Last Post
exception is never thrown in body of try statement annumol Pro Java 1 February 11th, 2008 06:53 AM
Database does not update, no exception thrown hexOffender VB Databases Basics 5 November 24th, 2006 03:28 PM
Exception has been thrown by the target of an invo rajasarkar ASP.NET 1.0 and 1.1 Professional 2 June 19th, 2006 03:19 AM
Autosuggest Exception thrown jellie BOOK: Professional Ajax ISBN: 978-0-471-77778-6 3 March 14th, 2006 10:19 AM
Exception thrown Invalid_Viewstate r_ganesh76 ASP.NET 1.0 and 1.1 Professional 1 December 2nd, 2005 04:32 PM





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