# Failing to catch exceptions from smart card reader

**URL:** <https://forum.inductiveautomation.com/t/failing-to-catch-exceptions-from-smart-card-reader/19819>\
**Category:** Ignition\
**Created:** [September 25, 2018, 7:03pm UTC](https://forum.inductiveautomation.com/t/failing-to-catch-exceptions-from-smart-card-reader/19819 "2018-09-25T19:03:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Claus\_Nielsen](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Claus\_Nielsen](https://forum.inductiveautomation.com/u/Claus_Nielsen)\
**Post date:** [September 25, 2018, 7:03pm UTC](https://forum.inductiveautomation.com/t/failing-to-catch-exceptions-from-smart-card-reader/19819/1 "2018-09-25T19:03:46Z")

</div>

Hi all,

Working on a script for using smart card readers with my application using the `javax.smartcardio` package. However the huge amount of exceptions thrown here when reading from the smart card is 50% of the time not catched in my try/except clause. The package usually throws one of these

`javax.smartcardio.CardException`  
`javax.smartcardio.CardNotPresentException`  
`sun.security.smartcardio.PCSCException`

The except clause looks like this

```auto
except (CardException, CardNotPresentException, PCSCException), e:
			
	# An error at this point should be related to reading the card. Notify card error
	#	by sending the error command to the terminal.
	self._terminal.NotifyError()
				
	if callable(self._onError):
		self._onError(e)

```

Any ideas anyone? As said, the clause works approx 50% of the times.

EDIT

Forgot to mention, tried catching `from java.lang.Exception` as all of the exceptions mentioned derives from this. This doesn’t seem to work either.

---

<div class="post-metadata">

**Author:** ![Claus\_Nielsen](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Claus\_Nielsen](https://forum.inductiveautomation.com/u/Claus_Nielsen)\
**Post date:** [September 25, 2018, 7:40pm UTC](https://forum.inductiveautomation.com/t/failing-to-catch-exceptions-from-smart-card-reader/19819/2 "2018-09-25T19:40:32Z")

</div>

And here’s a full trace of a card scan.

```auto
com.inductiveautomation.ignition.common.script.TimerScriptTask - Error executing global timer script: 
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
  File "<TimerScript:WeldingTool/Check Reader Input @100ms >", line 3, in <module>
  File "<module:project.smartcardio>", line 242, in ListenOnce
  File "<module:project.smartcardio>", line 314, in _listenOnce
  File "<module:project.smartcardio>", line 109, in NotifyError
	at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)

	at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)

	at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.lang.reflect.Method.invoke(Unknown Source)

javax.smartcardio.CardException: javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD

	at org.python.core.Py.JavaError(Py.java:495)
	at org.python.core.Py.JavaError(Py.java:488)
	at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:188)
	at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:204)
	at org.python.core.PyObject. __call__ (PyObject.java:404)
	at org.python.core.PyObject. __call__ (PyObject.java:408)
	at org.python.core.PyMethod. __call__ (PyMethod.java:124)
	at org.python.pycode._pyx2.NotifyError$12(<module:project.smartcardio>:110)
	at org.python.pycode._pyx2.call_function(<module:project.smartcardio>)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
	at org.python.core.PyFunction. __call__ (PyFunction.java:317)
	at org.python.core.PyMethod. __call__ (PyMethod.java:109)
	at org.python.pycode._pyx2._listenOnce$28(<module:project.smartcardio>:336)
	at org.python.pycode._pyx2.call_function(<module:project.smartcardio>)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:149)
	at org.python.core.PyFunction. __call__ (PyFunction.java:327)
	at org.python.core.PyMethod. __call__ (PyMethod.java:124)
	at org.python.pycode._pyx2.ListenOnce$27(<module:project.smartcardio>:255)
	at org.python.pycode._pyx2.call_function(<module:project.smartcardio>)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:141)
	at org.python.core.PyFunction. __call__ (PyFunction.java:327)
	at org.python.core.PyMethod. __call__ (PyMethod.java:124)
	at org.python.pycode._pyx0.f$0(<TimerScript:WeldingTool/Check Reader Input @100ms >:3)
	at org.python.pycode._pyx0.call_function(<TimerScript:WeldingTool/Check Reader Input @100ms >)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1275)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:636)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:603)
	at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:88)
	at java.util.TimerThread.mainLoop(Unknown Source)
	at java.util.TimerThread.run(Unknown Source)
Caused by: org.python.core.PyException: Traceback (most recent call last):
  File "<TimerScript:WeldingTool/Check Reader Input @100ms >", line 3, in <module>
  File "<module:project.smartcardio>", line 242, in ListenOnce
  File "<module:project.smartcardio>", line 314, in _listenOnce
  File "<module:project.smartcardio>", line 109, in NotifyError
	at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)

	at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)

	at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.lang.reflect.Method.invoke(Unknown Source)

javax.smartcardio.CardException: javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD

	... 36 common frames omitted
Caused by: javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
	at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)
	at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:186)
	... 33 common frames omitted
Caused by: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
	at sun.security.smartcardio.PCSC.SCardTransmit(Native Method)
	... 39 common frames omitted
21:29:25.199 [client-script-dedicated-timer-[WeldingTool/Check Reader Input]] ERROR com.inductiveautomation.ignition.common.script.TimerScriptTask - Error executing global timer script: WeldingTool/Check Reader Input @100ms . Repeat errors of this type will be logged as 'debug' messages.
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
  File "<TimerScript:WeldingTool/Check Reader Input @100ms >", line 3, in <module>
  File "<module:project.smartcardio>", line 242, in ListenOnce
  File "<module:project.smartcardio>", line 314, in _listenOnce
  File "<module:project.smartcardio>", line 109, in NotifyError
	at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)

	at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)

	at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.lang.reflect.Method.invoke(Unknown Source)

javax.smartcardio.CardException: javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD

	at org.python.core.Py.JavaError(Py.java:495)
	at org.python.core.Py.JavaError(Py.java:488)
	at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:188)
	at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:204)
	at org.python.core.PyObject. __call__ (PyObject.java:404)
	at org.python.core.PyObject. __call__ (PyObject.java:408)
	at org.python.core.PyMethod. __call__ (PyMethod.java:124)
	at org.python.pycode._pyx2.NotifyError$12(<module:project.smartcardio>:110)
	at org.python.pycode._pyx2.call_function(<module:project.smartcardio>)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
	at org.python.core.PyFunction. __call__ (PyFunction.java:317)
	at org.python.core.PyMethod. __call__ (PyMethod.java:109)
	at org.python.pycode._pyx2._listenOnce$28(<module:project.smartcardio>:336)
	at org.python.pycode._pyx2.call_function(<module:project.smartcardio>)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:149)
	at org.python.core.PyFunction. __call__ (PyFunction.java:327)
	at org.python.core.PyMethod. __call__ (PyMethod.java:124)
	at org.python.pycode._pyx2.ListenOnce$27(<module:project.smartcardio>:255)
	at org.python.pycode._pyx2.call_function(<module:project.smartcardio>)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
	at org.python.core.PyBaseCode.call(PyBaseCode.java:141)
	at org.python.core.PyFunction. __call__ (PyFunction.java:327)
	at org.python.core.PyMethod. __call__ (PyMethod.java:124)
	at org.python.pycode._pyx0.f$0(<TimerScript:WeldingTool/Check Reader Input @100ms >:3)
	at org.python.pycode._pyx0.call_function(<TimerScript:WeldingTool/Check Reader Input @100ms >)
	at org.python.core.PyTableCode.call(PyTableCode.java:165)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1275)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:636)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:603)
	at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:88)
	at java.util.TimerThread.mainLoop(Unknown Source)
	at java.util.TimerThread.run(Unknown Source)
Caused by: org.python.core.PyException: Traceback (most recent call last):
  File "<TimerScript:WeldingTool/Check Reader Input @100ms >", line 3, in <module>
  File "<module:project.smartcardio>", line 242, in ListenOnce
  File "<module:project.smartcardio>", line 314, in _listenOnce
  File "<module:project.smartcardio>", line 109, in NotifyError
	at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)

	at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)

	at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

	at java.lang.reflect.Method.invoke(Unknown Source)

javax.smartcardio.CardException: javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD

	... 36 common frames omitted
Caused by: javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
	at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)
	at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:186)
	... 33 common frames omitted
Caused by: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
	at sun.security.smartcardio.PCSC.SCardTransmit(Native Method)
	... 39 common frames omitted

```

The next thing that happens after this is the card is successfully read, however it seems like when the card is nearing the contactless scanner a bunch of exceptions is thrown.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 25, 2018, 7:44pm UTC](https://forum.inductiveautomation.com/t/failing-to-catch-exceptions-from-smart-card-reader/19819/3 "2018-09-25T19:44:30Z")

</div>

Consider catching all exceptions and then following the caused by chain to find your specific exceptions. Specifying exception types only filters on the outermost exception.

---

<div class="post-metadata">

**Author:** ![Claus\_Nielsen](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Claus\_Nielsen](https://forum.inductiveautomation.com/u/Claus_Nielsen)\
**Post date:** [September 25, 2018, 7:51pm UTC](https://forum.inductiveautomation.com/t/failing-to-catch-exceptions-from-smart-card-reader/19819/4 "2018-09-25T19:51:12Z")

</div>

How do I catch all exceptions and also get the exception for use in the except clause without knowing what to catch? As mentioned catching `java.lang.Exception` has the same issue with reliability and catching Pythons Exception never catches any of these.

---

<div class="post-metadata">

**Author:** ![Claus\_Nielsen](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Claus\_Nielsen](https://forum.inductiveautomation.com/u/Claus_Nielsen)\
**Post date:** [September 25, 2018, 7:54pm UTC](https://forum.inductiveautomation.com/t/failing-to-catch-exceptions-from-smart-card-reader/19819/5 "2018-09-25T19:54:06Z")

</div>

Anyway, after posting the trace I actually think I found the line of information I need to stop this. Have been looking at this all day 🙂

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 25, 2018, 7:56pm UTC](https://forum.inductiveautomation.com/t/failing-to-catch-exceptions-from-smart-card-reader/19819/6 "2018-09-25T19:56:56Z")

</div>

Set up two clauses, like so:

```auto
# Important! Do not use "from java.lang import Exception", as that will
# hide access to jython's Exception class.
import java.lang.Exception

def something():
	try:
		# do something
		pass
	except java.lang.Exception, e:
		# Handle java exceptions
		# follow e.cause, e.cause.cause, .... to examine nested exceptions
		pass
	except Exception, e:
		# Handle jython exceptions
		pass

```
