"I can't answer that question in the way you expect" ... that's something that may be said to you occasionally - you ask someone what suit a playing card is that they're holding and they cannot tell you because it is a joker, or you ask what number is written on a piece of paper when the paper is blank.
The way we handle situations like this in many modern programming languages is through excpetions - where a keyword such as
try is used to indicate a block of code that may potentially have a problem, and then we use
catch or
rescue to tell it what to do if something goes wrong.
Exceptions are blocks, and although you may not think so at first the scope (size) of block is every bit as important as the scope of an
if or
while block.
Sometimes, exceptions will be processed in there entirety by the function / method in which the problem occurs, but at other times you'll want to throw the excpetion back up to the calling level; in that case, you'll need to put a try block in the calling code too. There's a new example of this (in Ruby) on our web site from today's
Ruby Programming Course ... and the example is
[here]. We have an example showing how it's coded in Python
[here], and one from C++
[here}. We've got examples of the format of exceptions in Java on our site too - see
[here].
(written 2010-02-03)
Associated topics are indexed under
R111 - Ruby - Exceptions. [3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
[3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
[3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
[3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
[2615] String to number conversion with error trapping in Ruby - (2010-02-01)
[1875] What are exceptions - Python based answer - (2008-11-08)
Y109 - Python - Exceptions [3441] Pressing ^C in a Python program. Also Progress Bar. - (2011-09-15)
[2998] Using an exception to initialise a static variable in a Python function / method - (2010-10-13)
[2994] Python - some common questions answered in code examples - (2010-10-10)
[2408] Robust user input (exception handling) example in Python - (2009-09-17)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2281] Python - using exceptions to set a fallback - (2009-07-12)
[2018] UnboundLocalError - Python Message - (2009-01-31)
[1236] Trying things in Python - (2007-06-18)
[1042] Nested exceptions in Python - (2007-01-18)
[381] Exceptions in Python - (2005-07-17)
C236 - C and C based languages - Exceptions [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3068] Throwing your own exception in C++, and catching it - (2010-11-24)
[799] The case for exceptions - (2006-07-11)
J712 - Java - Exceptions [3048] String handling - from first steps to practical examples - (2010-11-13)
[3045] After Course Resources - do we publish sample answers. Example from Java Exceptions module. - (2010-11-13)
[2862] Fail Safe Error Handling in Java via Exceptions - (2010-07-09)
[2420] Exceptions in Java - why and how - (2009-09-24)
[1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
Some other Articles
On Malachite GreenBoth feet on the same pavementSkyline and looking up in LondonObject Oriented Ruby - new examplesHandling unusual and error conditions - exceptionsPassing code to procedures and yield in RubyWhat are Ruby Symbols?Comparing floating point numbers - a word of caution and a solution