www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to handle exceptions right?

reply dog2002 <742617000027 aaathats3as.com> writes:
I want to call functions according to an exception. In C there is 
the errno function. But if I use try...catch, it shows a trace or 
a message. How do I check an exception type? For example:

std.socket.SocketOSException std/socket.d(2857): Unable to 
connect socket: Connection refused
----------------
??:? [0x459ec5]
??:? [0x4646e6]
??:? [0x44743d]
??:? [0x42c833]
??:? [0x404273]
??:? [0x404cf8]
??:? [0x440a43]
??:? [0x7fdeccecc3f8]
??:? clone [0x7fdeccc98b52]

or

Unable to connect socket: Connection refused

So do I just need to check the exception string? Or there is 
something like errno in C?
Mar 08
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
https://github.com/dlang/phobos/blob/master/std/socket.d#L190

Might be of some use to you
Mar 08
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 3/8/21 8:05 AM, dog2002 wrote:
 I want to call functions according to an exception. In C there is the 
 errno function. But if I use try...catch, it shows a trace or a message. 
 How do I check an exception type? For example:
 
 std.socket.SocketOSException std/socket.d(2857): Unable to connect 
 socket: Connection refused
 ----------------
 ??:? [0x459ec5]
 ??:? [0x4646e6]
 ??:? [0x44743d]
 ??:? [0x42c833]
 ??:? [0x404273]
 ??:? [0x404cf8]
 ??:? [0x440a43]
 ??:? [0x7fdeccecc3f8]
 ??:? clone [0x7fdeccc98b52]
 
 or
 
 Unable to connect socket: Connection refused
 
 So do I just need to check the exception string? Or there is something 
 like errno in C?
In the case where you are actually looking to see what *type* the exception is: try { ... // socket code } catch(SocketException e) { // it's a socket exception here } catch(Exception e) { // otherwise, it's a different kind of exception } -Steve
Mar 08