"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 as below, or enter http://melksh.am/nnnn for individual articles
Y109 - Python - Exceptions [381] Exceptions in Python - (2005-07-17)
[1042] Nested exceptions in Python - (2007-01-18)
[1236] Trying things in Python - (2007-06-18)
[2018] UnboundLocalError - Python Message - (2009-01-31)
[2281] Python - using exceptions to set a fallback - (2009-07-12)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2408] Robust user input (exception handling) example in Python - (2009-09-17)
[2994] Python - some common questions answered in code examples - (2010-10-10)
[2998] Using an exception to initialise a static variable in a Python function / method - (2010-10-13)
[3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
[3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
[3441] Pressing ^C in a Python program. Also Progress Bar. - (2011-09-15)
[3664] Error checking in a Python program - making your program robust via exceptions - (2012-03-22)
[3913] How many times ... has this loco headed west through Tenby? - Python exceptions - (2012-11-05)
[3930] Reporting the full stack trace when you catch a Python exception - (2012-11-22)
[4029] Exception, Lambda, Generator, Slice, Dict - examples in one Python program - (2013-03-04)
[4161] Python varables - checking existance, and call by name or by value? - (2013-08-27)
[4444] Elements of an exception in Python - try, except, else, finally - (2015-02-28)
R111 - Ruby - Exceptions. [1875] What are exceptions - Python based answer - (2008-11-08)
[2615] String to number conversion with error trapping in Ruby - (2010-02-01)
[2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
[3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
[4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17)
[4675] Exceptions in Ruby - throwing, catching and using - (2016-05-17)
J712 - Java - Exceptions [1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
[2420] Exceptions in Java - why and how - (2009-09-24)
[2862] Fail Safe Error Handling in Java via Exceptions - (2010-07-09)
[3045] After Course Resources - do we publish sample answers. Example from Java Exceptions module. - (2010-11-13)
[3048] String handling - from first steps to practical examples - (2010-11-13)
[4350] Keyboard reader for Java programming newcomers - (2014-12-12)
[4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
C236 - C and C based languages - Exceptions [799] The case for exceptions - (2006-07-11)
[3068] Throwing your own exception in C++, and catching it - (2010-11-24)
[3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
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