Skip to content

Instantly share code, notes, and snippets.

@DanielTakeshi
Last active May 12, 2019 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielTakeshi/2a5423ded016f06b9aeb063185d2f97b to your computer and use it in GitHub Desktop.
Save DanielTakeshi/2a5423ded016f06b9aeb063185d2f97b to your computer and use it in GitHub Desktop.
Exception Testing in Python
"""Some exception testing.
The code in the `try` block will stop as soon as any exception is encountered.
https://realpython.com/python-exceptions/
https://realpython.com/the-most-diabolical-python-antipattern/
"""
a = 5
# This is bad, will not print the actual exception error message.
try:
b = a / 0.0
except:
print('BAD, no exception listed! oops, divide by zero\n')
# If you run this, you get an error because this is NOT a ValueError.
# That's why I am commenting this out --- it terminates the program.
#try:
# b = a / 0.0
#except ValueError:
# print('ValueError. oops, divide by zero')
# Fortunately, this will work.
try:
b = a / 0.0
except ZeroDivisionError:
print('ZeroDivisionError. oops, divide by zero')
print('And the program will proceed from here\n')
# And this is probably better because we actually print the `e`.
try:
b = a / 0.0
except ZeroDivisionError as e:
print('ZeroDivisionError. oops, divide by zero. Here is the exception object:')
print(e)
print("")
# Actually, might as well do python logging (easiest that way).
# The 'caught an error' text will appear within the logger message.
# This error will go and print the stack trace as well.
import logging
try:
b = a / 0.0
except ZeroDivisionError as e:
logging.exception('caught an error')
print('and code logic can still proceed from here\n\n')
import numpy as np
import scipy
from scipy.spatial import ConvexHull
points = np.array([ [0,0], [1,0], [2,0] ])
try:
hull = ConvexHull(points)
except scipy.spatial.qhull.QhullError as e:
logging.exception(e)
print('\nwell, we caught an error ... lets proceed from here')
points = np.array([ [0,0], [1,0], [2,0], [3,0], [4,0]])
try:
hull = ConvexHull(points)
except scipy.spatial.qhull.QhullError as e:
logging.exception(e)
print('\nwell, we caught an error ... lets proceed from here')
points = np.array([ [0,0], [1,0], [2,0], [3,0], [4,1]])
try:
hull = ConvexHull(points)
print('\nvolume: {}'.format(hull.volume))
except scipy.spatial.qhull.QhullError as e:
logging.exception(e)
print('\nshould work ...')
@DanielTakeshi
Copy link
Author

DanielTakeshi commented May 12, 2019

BAD, no exception listed! oops, divide by zero

ZeroDivisionError. oops, divide by zero
And the program will proceed from here

ZeroDivisionError. oops, divide by zero. Here is the exception object:
float division by zero

ERROR:root:caught an error
Traceback (most recent call last):
  File "catch_test.py", line 48, in <module>
    b = a / 0.0
ZeroDivisionError: float division by zero
and code logic can still proceed from here


ERROR:root:QH6154 Qhull precision error: Initial simplex is flat (facet 1 is coplanar with the interior point)

While executing:  | qhull i Qt
Options selected for Qhull 2015.2.r 2016/01/18:
  run-id 34284603  incidence  Qtriangulate  _pre-merge  _zero-centrum
  _max-width  2  Error-roundoff 1.3e-15  _one-merge 6.7e-15
  _near-inside 3.4e-14  Visible-distance 2.7e-15  U-coplanar-distance 2.7e-15
  Width-outside 5.4e-15  _wide-facet 1.6e-14

precision problems (corrected unless 'Q0' or an error)
      2 flipped facets

The input to qhull appears to be less than 2 dimensional, or a
computation has overflowed.

Qhull could not construct a clearly convex simplex from points:
- p1(v2):     1     0
- p2(v1):     2     0
- p0(v0):     0     0

The center point is coplanar with a facet, or a vertex is coplanar
with a neighboring facet.  The maximum round off error for
computing distances is 1.3e-15.  The center point, facets and distances
to the center point are as follows:

center point        1        0

facet p2 p0 distance=    0
facet p1 p0 distance=    0
facet p1 p2 distance=    0

These points either have a maximum or minimum x-coordinate, or
they maximize the determinant for k coordinates.  Trial points
are first selected from points that maximize a coordinate.

The min and max coordinates for each dimension are:
  0:         0         2  difference=    2
  1:         0         0  difference=    0

If the input should be full dimensional, you have several options that
may determine an initial simplex:
  - use 'QJ'  to joggle the input and make it full dimensional
  - use 'QbB' to scale the points to the unit cube
  - use 'QR0' to randomly rotate the input for different maximum points
  - use 'Qs'  to search all points for the initial simplex
  - use 'En'  to specify a maximum roundoff error less than 1.3e-15.
  - trace execution with 'T3' to see the determinant for each point.

If the input is lower dimensional:
  - use 'QJ' to joggle the input and make it full dimensional
  - use 'Qbk:0Bk:0' to delete coordinate k from the input.  You should
    pick the coordinate with the least range.  The hull will have the
    correct topology.
  - determine the flat containing the points, rotate the points
    into a coordinate plane, and delete the other coordinates.
  - add one or more points to make the input full dimensional.
Traceback (most recent call last):
  File "catch_test.py", line 60, in <module>
    hull = ConvexHull(points)
  File "qhull.pyx", line 2359, in scipy.spatial.qhull.ConvexHull.__init__
  File "qhull.pyx", line 354, in scipy.spatial.qhull._Qhull.__init__
scipy.spatial.qhull.QhullError: QH6154 Qhull precision error: Initial simplex is flat (facet 1 is coplanar with the interior point)

While executing:  | qhull i Qt
Options selected for Qhull 2015.2.r 2016/01/18:
  run-id 34284603  incidence  Qtriangulate  _pre-merge  _zero-centrum
  _max-width  2  Error-roundoff 1.3e-15  _one-merge 6.7e-15
  _near-inside 3.4e-14  Visible-distance 2.7e-15  U-coplanar-distance 2.7e-15
  Width-outside 5.4e-15  _wide-facet 1.6e-14

precision problems (corrected unless 'Q0' or an error)
      2 flipped facets

The input to qhull appears to be less than 2 dimensional, or a
computation has overflowed.

Qhull could not construct a clearly convex simplex from points:
- p1(v2):     1     0
- p2(v1):     2     0
- p0(v0):     0     0

The center point is coplanar with a facet, or a vertex is coplanar
with a neighboring facet.  The maximum round off error for
computing distances is 1.3e-15.  The center point, facets and distances
to the center point are as follows:

center point        1        0

facet p2 p0 distance=    0
facet p1 p0 distance=    0
facet p1 p2 distance=    0

These points either have a maximum or minimum x-coordinate, or
they maximize the determinant for k coordinates.  Trial points
are first selected from points that maximize a coordinate.

The min and max coordinates for each dimension are:
  0:         0         2  difference=    2
  1:         0         0  difference=    0

If the input should be full dimensional, you have several options that
may determine an initial simplex:
  - use 'QJ'  to joggle the input and make it full dimensional
  - use 'QbB' to scale the points to the unit cube
  - use 'QR0' to randomly rotate the input for different maximum points
  - use 'Qs'  to search all points for the initial simplex
  - use 'En'  to specify a maximum roundoff error less than 1.3e-15.
  - trace execution with 'T3' to see the determinant for each point.

If the input is lower dimensional:
  - use 'QJ' to joggle the input and make it full dimensional
  - use 'Qbk:0Bk:0' to delete coordinate k from the input.  You should
    pick the coordinate with the least range.  The hull will have the
    correct topology.
  - determine the flat containing the points, rotate the points
    into a coordinate plane, and delete the other coordinates.
  - add one or more points to make the input full dimensional.


well, we caught an error ... lets proceed from here
ERROR:root:QH6154 Qhull precision error: Initial simplex is flat (facet 1 is coplanar with the interior point)

While executing:  | qhull i Qt
Options selected for Qhull 2015.2.r 2016/01/18:
  run-id 34284603  incidence  Qtriangulate  _pre-merge  _zero-centrum
  _max-width  4  Error-roundoff 2.7e-15  _one-merge 1.3e-14
  _near-inside 6.7e-14  Visible-distance 5.4e-15  U-coplanar-distance 5.4e-15
  Width-outside 1.1e-14  _wide-facet 3.2e-14

precision problems (corrected unless 'Q0' or an error)
      2 flipped facets

The input to qhull appears to be less than 2 dimensional, or a
computation has overflowed.

Qhull could not construct a clearly convex simplex from points:
- p1(v2):     1     0
- p4(v1):     4     0
- p0(v0):     0     0

The center point is coplanar with a facet, or a vertex is coplanar
with a neighboring facet.  The maximum round off error for
computing distances is 2.7e-15.  The center point, facets and distances
to the center point are as follows:

center point    1.667        0

facet p4 p0 distance=    0
facet p1 p0 distance=    0
facet p1 p4 distance=    0

These points either have a maximum or minimum x-coordinate, or
they maximize the determinant for k coordinates.  Trial points
are first selected from points that maximize a coordinate.

The min and max coordinates for each dimension are:
  0:         0         4  difference=    4
  1:         0         0  difference=    0

If the input should be full dimensional, you have several options that
may determine an initial simplex:
  - use 'QJ'  to joggle the input and make it full dimensional
  - use 'QbB' to scale the points to the unit cube
  - use 'QR0' to randomly rotate the input for different maximum points
  - use 'Qs'  to search all points for the initial simplex
  - use 'En'  to specify a maximum roundoff error less than 2.7e-15.
  - trace execution with 'T3' to see the determinant for each point.

If the input is lower dimensional:
  - use 'QJ' to joggle the input and make it full dimensional
  - use 'Qbk:0Bk:0' to delete coordinate k from the input.  You should
    pick the coordinate with the least range.  The hull will have the
    correct topology.
  - determine the flat containing the points, rotate the points
    into a coordinate plane, and delete the other coordinates.
  - add one or more points to make the input full dimensional.
Traceback (most recent call last):
  File "catch_test.py", line 69, in <module>
    hull = ConvexHull(points)
  File "qhull.pyx", line 2359, in scipy.spatial.qhull.ConvexHull.__init__
  File "qhull.pyx", line 354, in scipy.spatial.qhull._Qhull.__init__
scipy.spatial.qhull.QhullError: QH6154 Qhull precision error: Initial simplex is flat (facet 1 is coplanar with the interior point)

While executing:  | qhull i Qt
Options selected for Qhull 2015.2.r 2016/01/18:
  run-id 34284603  incidence  Qtriangulate  _pre-merge  _zero-centrum
  _max-width  4  Error-roundoff 2.7e-15  _one-merge 1.3e-14
  _near-inside 6.7e-14  Visible-distance 5.4e-15  U-coplanar-distance 5.4e-15
  Width-outside 1.1e-14  _wide-facet 3.2e-14

precision problems (corrected unless 'Q0' or an error)
      2 flipped facets

The input to qhull appears to be less than 2 dimensional, or a
computation has overflowed.

Qhull could not construct a clearly convex simplex from points:
- p1(v2):     1     0
- p4(v1):     4     0
- p0(v0):     0     0

The center point is coplanar with a facet, or a vertex is coplanar
with a neighboring facet.  The maximum round off error for
computing distances is 2.7e-15.  The center point, facets and distances
to the center point are as follows:

center point    1.667        0

facet p4 p0 distance=    0
facet p1 p0 distance=    0
facet p1 p4 distance=    0

These points either have a maximum or minimum x-coordinate, or
they maximize the determinant for k coordinates.  Trial points
are first selected from points that maximize a coordinate.

The min and max coordinates for each dimension are:
  0:         0         4  difference=    4
  1:         0         0  difference=    0

If the input should be full dimensional, you have several options that
may determine an initial simplex:
  - use 'QJ'  to joggle the input and make it full dimensional
  - use 'QbB' to scale the points to the unit cube
  - use 'QR0' to randomly rotate the input for different maximum points
  - use 'Qs'  to search all points for the initial simplex
  - use 'En'  to specify a maximum roundoff error less than 2.7e-15.
  - trace execution with 'T3' to see the determinant for each point.

If the input is lower dimensional:
  - use 'QJ' to joggle the input and make it full dimensional
  - use 'Qbk:0Bk:0' to delete coordinate k from the input.  You should
    pick the coordinate with the least range.  The hull will have the
    correct topology.
  - determine the flat containing the points, rotate the points
    into a coordinate plane, and delete the other coordinates.
  - add one or more points to make the input full dimensional.


well, we caught an error ... lets proceed from here

volume: 1.5

should work ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment