<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://teaching.healthtech.dtu.dk:443/22116/index.php?action=history&amp;feed=atom&amp;title=Example_code_-_Exceptions</id>
	<title>Example code - Exceptions - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://teaching.healthtech.dtu.dk:443/22116/index.php?action=history&amp;feed=atom&amp;title=Example_code_-_Exceptions"/>
	<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22116/index.php?title=Example_code_-_Exceptions&amp;action=history"/>
	<updated>2026-04-29T10:05:25Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://teaching.healthtech.dtu.dk:443/22116/index.php?title=Example_code_-_Exceptions&amp;diff=66&amp;oldid=prev</id>
		<title>WikiSysop: Created page with &quot;== Determining input type == How to determine the input with try/expect. This is not really how it is done in practice, but it is still good to understand the ideas, because sooner or later you run into a situation where parts of this is useful. &lt;pre&gt; inputstring = input(&quot;Enter something and I will guess what it is: &quot;) try:     # This assignment will succeed     mytype = &#039;string&#039;     # This may fail, in such case our exception handler is called     result = float(inputst...&quot;</title>
		<link rel="alternate" type="text/html" href="https://teaching.healthtech.dtu.dk:443/22116/index.php?title=Example_code_-_Exceptions&amp;diff=66&amp;oldid=prev"/>
		<updated>2025-08-26T09:14:45Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Determining input type == How to determine the input with try/expect. This is not really how it is done in practice, but it is still good to understand the ideas, because sooner or later you run into a situation where parts of this is useful. &amp;lt;pre&amp;gt; inputstring = input(&amp;quot;Enter something and I will guess what it is: &amp;quot;) try:     # This assignment will succeed     mytype = &amp;#039;string&amp;#039;     # This may fail, in such case our exception handler is called     result = float(inputst...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Determining input type ==&lt;br /&gt;
How to determine the input with try/expect. This is not really how it is done in practice, but it is still good to understand the ideas, because sooner or later you run into a situation where parts of this is useful.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
inputstring = input(&amp;quot;Enter something and I will guess what it is: &amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    # This assignment will succeed&lt;br /&gt;
    mytype = &amp;#039;string&amp;#039;&lt;br /&gt;
    # This may fail, in such case our exception handler is called&lt;br /&gt;
    result = float(inputstring)&lt;br /&gt;
    mytype = &amp;#039;floating point&amp;#039;&lt;br /&gt;
    # This may fail, in such case our exception handler is called&lt;br /&gt;
    result = int(inputstring)&lt;br /&gt;
    mytype = &amp;#039;integer&amp;#039;&lt;br /&gt;
# The exception handler, do nothing&lt;br /&gt;
except ValueError:&lt;br /&gt;
    pass&lt;br /&gt;
# because we have already found the correct type, so print it&lt;br /&gt;
print(&amp;quot;It is a&amp;quot;, mytype)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Show first 10 lines in a file ==&lt;br /&gt;
Using exceptions to catch errors in the file name. This is very useful when you run the program from the command line. When using Jupyter Notebook, this is unfortunately not so practical as it kills the python kernel.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import sys&lt;br /&gt;
filename = input(&amp;quot;Enter a filename: &amp;quot;)&lt;br /&gt;
try:&lt;br /&gt;
    # Will fail if there is no readable file by that name&lt;br /&gt;
    infile = open(filename, &amp;#039;r&amp;#039;)&lt;br /&gt;
except IOError as err:&lt;br /&gt;
    print(&amp;quot;Can not open&amp;quot;, filename, &amp;quot;Reason:&amp;quot;, str(err))&lt;br /&gt;
    sys.exit(1)&lt;br /&gt;
count = 0&lt;br /&gt;
line = infile.readline()&lt;br /&gt;
while line != &amp;quot;&amp;quot; and count &amp;lt; 10:&lt;br /&gt;
    print(line, end=&amp;#039;&amp;#039;)&lt;br /&gt;
    count += 1&lt;br /&gt;
    line = infile.readline()&lt;br /&gt;
print(&amp;quot;This is the first&amp;quot;, count, &amp;quot;lines of&amp;quot;, filename)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>WikiSysop</name></author>
	</entry>
</feed>