Skip to content

Commit 1186f64

Browse files
committed
Finished adding content. Awaiting review before making addtional changes.
1 parent 919c057 commit 1186f64

1 file changed

Lines changed: 78 additions & 3 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
282282
</program>
283283

284284
<p>
285-
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 try/except (Python) or try/catch (Java) blocks within a <c>while</c> loop that iterates until the user enter the correct code. Adding try/except blocks and a <c>while</c> loop will look something like this:
285+
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 try/except (Python) or try/catch (Java) blocks within a <c>while</c> loop that iterates until the user enter the correct code. Adding try/except blocks and a <c>while</c> loop to the Python code will look something like this:
286286
</p>
287287

288288
<program interactive="activecode" language="python" xml:id="square-input-exception-python">
@@ -298,6 +298,10 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
298298
</code>
299299
</program>
300300

301+
<p>
302+
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.
303+
</p>
304+
301305
<program interactive="activecode" language="java" xml:id="square-input-exception-java">
302306
<code>
303307
import java.util.Scanner;
@@ -316,14 +320,85 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
316320
break;
317321
} catch (InputMismatchException e) {
318322
System.out.println("That was not a valid number. Please try again: ");
319-
scanner.nextLine(); // Clear the invalid input
323+
scanner.nextLine();
320324
}
321325
}
322326
}
323327
}
324328
</code>
325329
</program>
326-
330+
331+
<p>
332+
Firstly, let's talk about the extra import alongside the <c>Scanner</c> import. In Java, any exception that isn't part of the <c>java.lang</c> class or is a checked exception must be explicitly imported to be used with try/catch blocks. If you ran the previous Java codeblock without try/catch 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 try/catch code block above will lead to compilation errors.
333+
</p>
334+
335+
<p>
336+
<idx>checked exception</idx>
337+
<idx>unchecked exception</idx>
338+
Exceptions in Java fall under two categories: checked and unchecked. <term>Checked exceptions</term> must be explicitly imported and declared along with try/catch blocks for a program to compile. <term>Unchecked exceptions</term> do not need to be imported unless try/catch blocks are implemented for them (except for <c>java.lang</c> exceptions). <c>InputMismatchException</c> is an unchecked exception that is not part of the <c>java.lang</c> library, so it is only included if try/catch blocks declare it. Here are some common exceptions used with try/catch blocks:
339+
</p>
340+
341+
<table>
342+
<title>Exceptions</title>
343+
<tabular>
344+
<row>
345+
<cell><term>Exception</term></cell>
346+
<cell><term>Package</term></cell>
347+
<cell><term>Description</term></cell>
348+
</row>
349+
<row>
350+
<cell><c>IOException</c></cell>
351+
<cell><c>java.io</c></cell>
352+
<cell>Thrown when an I/O operation fails (e.g., reading or writing a file).</cell>
353+
</row>
354+
<row>
355+
<cell><c>FileNotFoundException</c></cell>
356+
<cell><c>java.io</c></cell>
357+
<cell>Thrown when an attempt to open a file denoted by a pathname has failed.</cell>
358+
</row>
359+
<row>
360+
<cell><c>ParseException</c></cell>
361+
<cell><c>java.text</c></cell>
362+
<cell>Thrown when parsing a string into a date, number, etc. fails (e.g., wrong format).</cell>
363+
</row>
364+
<row>
365+
<cell><c>NoSuchMethodException</c></cell>
366+
<cell><c>java.lang</c></cell>
367+
<cell>Thrown when a particular method cannot be found via reflection.</cell>
368+
</row>
369+
<row>
370+
<cell><c>InputMismatchException</c></cell>
371+
<cell><c>java.util</c></cell>
372+
<cell>Thrown when <c>Scanner</c> input doesn’t match the expected data type.</cell>
373+
</row>
374+
<row>
375+
<cell><c>SQLException</c></cell>
376+
<cell><c>java.sql</c></cell>
377+
<cell>Thrown when a database access error occurs (e.g., invalid SQL query, bad connection).</cell>
378+
</row>
379+
<row>
380+
<cell><c>InstantiationException</c></cell>
381+
<cell><c>java.lang</c></cell>
382+
<cell>Thrown when trying to create an instance of an abstract class or interface.</cell>
383+
</row>
384+
<row>
385+
<cell><c>IllegalAccessException</c></cell>
386+
<cell><c>java.lang</c></cell>
387+
<cell>Thrown when a reflection operation tries to access a field or method it doesn't have permission for.</cell>
388+
</row>
389+
</tabular>
390+
</table>
391+
392+
<p>
393+
Next, we will look into the try/catch blocks themselves. As with most other structures in Java, the blocks must be encased with braces <c>{}</c>. The most important part of this code is, after <c>catch</c>, there is a set of parenthesis with an exception type and a variable name <c>catch (InputMismatchException e)</c>. This is where we declare a <c>InputMismatchException</c> exception and name it with the variable name <c>e</c>. It is common practice to name exception variables <c>e</c> in this manner.
394+
</p>
395+
396+
<note>
397+
<p>
398+
Typically, Java is more "strict" than Python. Variable data types must be declared along with the variable, braces must be used, etc. A notable exception occurs in print statements as seen in the code in this section. In Python, strings can only be concatenated with other strings, but in Java, strings can be concatenated with other data types.
399+
</p>
400+
</note>
401+
327402
</section>
328403

329404
<section xml:id="boolean-operators">

0 commit comments

Comments
 (0)