Skip to content

Commit 72cfca5

Browse files
authored
Merge pull request #274 from habibasorour/issue_256_listing
Issue 256 fixed: 4.5 listing
2 parents 22fa43f + ebaaf16 commit 72cfca5

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,24 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
261261
<title>Exception Handling</title>
262262

263263
<p>
264-
In Python, if you want a program to continue running when an error has occurred, you can use <c>try-except</c> blocks to handle exceptions. If you wanted to write a program that asks the user to enter a whole number and then squares that number, you could use the following code to do so:
264+
In Python, if you want a program to continue running when an error has occurred, you can use <c>try-except</c> blocks to handle exceptions. If you wanted to write a program that asks the user to enter a whole number and then squares that number, you could use the following code to do so. <xref ref="python-square-input" text="type-global"/> shows the Python code to achieve this.
265265
</p>
266266

267-
<program xml:id="python-square-input" interactive="activecode" language="python">
267+
<listing xml:id="python-square-input">
268+
<program interactive="activecode" language="python">
268269
<code>
269270
number = int(input("Please enter a whole number: "))
270271
squared = number ** 2
271272
print("Your number squared is " + str(squared))
272273
</code>
273274
</program>
275+
</listing>
274276

275277
<p>
276-
The Java code that would perform the same task is a little more complex and utilizes the <c>Scanner</c> class for input.
278+
<xref ref="java-square-input" text="type-global"/> shows the Java code that would perform the same task. It is a little more complex and utilizes the <c>Scanner</c> class for input.
277279
</p>
278-
279-
<program xml:id="java-square-input" interactive="activecode" language="java">
280+
<listing xml:id="java-square-input">
281+
<program interactive="activecode" language="java">
280282
<code>
281283
import java.util.Scanner;
282284

@@ -293,12 +295,13 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
293295
}
294296
</code>
295297
</program>
298+
</listing>
296299

297300
<p>
298-
This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add <c>try-except</c> (Python) or <c>try-catch</c> (Java) blocks within a <c>while</c> loop that iterates until the user enter the correct code. Adding <c>try-except</c> blocks and a <c>while</c> loop to the Python code will look something like this:
301+
This code works well, but will end with an exception if the user types anything other than a whole number (such as 12.5 or two). If we wanted to ensure the code will continue to run until the user enters the correct format, we could add <c>try-except</c> (Python) or <c>try-catch</c> (Java) blocks within a <c>while</c> loop that iterates until the user enter the correct code. While try-except blocks aren't strictly required in Python, <xref ref="python-square-input-exception" text="type-global"/> shows how using them alongside a while loop makes the code more robust.
299302
</p>
300-
301-
<program xml:id="python-square-input-exception" interactive="activecode" language="python">
303+
<listing xml:id="python-square-input-exception">
304+
<program interactive="activecode" language="python">
302305
<code>
303306
while True:
304307
try:
@@ -310,12 +313,13 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
310313
print("That was not a valid number. Please try again: ")
311314
</code>
312315
</program>
316+
</listing>
313317

314318
<p>
315-
Now that we have Python code that will continuously prompt the user until they enter a whole number, let's look at Java code that accomplishes the same task. Like most other equivalent Java code blocks, this code has a lot of extra bits that are necessary to get working code.
319+
Now that we have Python code that will continuously prompt the user until they enter a whole number, <xref ref="java-square-input-exception" text="type-global"/> shows the Java code that accomplishes the same task. Like most other equivalent Java code blocks, this code has a lot of extra bits that are necessary to get working code.
316320
</p>
317-
318-
<program xml:id="java-square-input-exception" interactive="activecode" language="java">
321+
<listing xml:id="java-square-input-exception">
322+
<program interactive="activecode" language="java">
319323
<code>
320324
import java.util.Scanner;
321325
import java.util.InputMismatchException;
@@ -340,6 +344,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
340344
}
341345
</code> <stdin> </stdin> <tests> </tests>
342346
</program>
347+
</listing>
343348

344349
<p>
345350
Firstly, let's talk about the extra import alongside the <c>Scanner</c> import. In Java, we need to import <c>InputMismatchException</c> because it's not automatically available like basic exceptions. This is different from Python where most exceptions are readily accessible. If you ran the previous Java codeblock without <c>try-catch</c> blocks and entered an erroneous input, you would have got an <c>InputMismatchException</c> exception despite not having imported this class. That being said, removing the explicit import of this library for the <c>try-catch</c> code block above will lead to compilation errors.

0 commit comments

Comments
 (0)