.net - Try-catch on PostgreSQL not catching exception -
i testing postgresql connection using npgsql . want know happens when make mistake ,for example wrong connection string ,database down .... etc. made simple program test npgsqlexceptions try statement not catching exception:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using npgsql; namespace windowsformsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { npgsqlconnection conn = new npgsqlconnection("server=127.0.0.1;port=5432;user id=postgres;password=knbvbnv;database=testdb;"); try { conn.open(); } catch (npgsqlexception ee) { console.writeline("======== ee.code ==============="); console.writeline(ee.code); console.writeline("======== ee.columnname ==============="); console.writeline(ee.columnname); console.writeline("======== ee.constraintname ==============="); console.writeline(ee.constraintname); console.writeline("======== ee.datatypename ==============="); console.writeline(ee.datatypename); console.writeline("======== detail ==============="); console.writeline(ee.detail); console.writeline("======== errorcode ==============="); console.writeline(ee.errorcode); console.writeline("======== hresult ==============="); console.writeline(ee.hresult); console.writeline("======== internalquery ==============="); console.writeline(ee.internalquery); console.writeline("======== message ==============="); console.writeline(ee.message); console.writeline("======== messagetext ==============="); console.writeline(ee.messagetext); console.writeline("======== position ==============="); console.writeline(ee.position); console.writeline("======== severity ==============="); console.writeline(ee.severity); console.writeline("======== targetsite ==============="); console.writeline(ee.targetsite); console.writeline("======== ==============="); console.writeline(ee.where); console.writeline("======== ==============="); } } } }
how come try not catch errors ?
exception in visual studio : unhandled exception of type 'system.net.sockets.socketexception' occurred in npgsql.dll
additional information: no connection made because target machine actively refused it
it's throwing socketexception
. you're catching npgsqlexception
. if add secondary catch socketexception
, it'll catch.
Comments
Post a Comment