Reputation: 5789
when I call myFunction(test)
it goes correctly but throws java exception
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
and I would like to catch it and/or catch any java exception.
I tried :
try
myFunction(test)
catch exp
mess = exp.message
end
but the mess
is empty ...
how could I check that myFunction
doesn't catch any java exception ?
Upvotes: 1
Views: 844
Reputation: 124563
The ability to access Java exceptions from MATLAB was added in R2012b. If I'm not mistaken, prior versions could only catch exceptions in a general manner without specific error information about the Java error.
try
openStream(java.net.URL('http://non.existant'))
catch ME
end
The exception caught is a MATLAB object that wraps the original Java exception:
>> ME
ME =
JavaException with properties:
ExceptionObject: [1x1 java.net.UnknownHostException]
identifier: 'MATLAB:Java:GenericException'
message: 'Java exception occurred:
java.net.UnknownHostException: non.existant
at java.net.AbstractPlainSocketImpl.connect(Unk...'
cause: {}
stack: [0x1 struct]
so we can access the entire error stack trace:
>> printStackTrace(ME.ExceptionObject)
java.net.UnknownHostException: non.existant
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
Upvotes: 0
Reputation: 147
Either any of below can be use
Instead of "exp" use ME, it is MATLABException Variable which actually catch and store in Message
use "ERROR" instead of "exp" and and then display Message directly on MATLAB command window
Upvotes: 0