You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I am trying to understand how exceptions are handled in Crystal-sqlite3.
Given the following code :
require "sqlite3"
DBCONN = DB.connect "sqlite3://%3Amemory%3A"
begin
DBCONN.exec "create table test ( id integer primary key, code text not null unique)"
DBCONN.exec "insert into test(code) values('A')"
DBCONN.exec "insert into test(code) values('A')"
rescue e
if e.message =~ /UNIQUE constraint failed/i
puts ("Already in database")
else
puts (e.message)
end
ensure
DBCONN.close
end
output is :
Already in database
Unhandled exception: UNIQUE constraint failed: test.code (SQLite3::Exception)
from lib/sqlite3/src/sqlite3/statement.cr:81:5 in 'check'
from lib/sqlite3/src/sqlite3/statement.cr:37:5 in 'do_close'
from lib/db/src/db/disposable.cr:11:7 in 'close'
from lib/db/src/db/connection.cr:60:38 in 'do_close'
from lib/sqlite3/src/sqlite3/connection.cr:34:5 in 'do_close'
from lib/db/src/db/disposable.cr:11:7 in 'close'
from test1a.cr:14:3 in '__crystal_main'
from /usr/lib/crystal/crystal/main.cr:97:5 in 'main_user_code'
from /usr/lib/crystal/crystal/main.cr:86:7 in 'main'
from /usr/lib/crystal/crystal/main.cr:106:3 in 'main'
from __libc_start_main
from _start
from ???
Why does Crystal tell me the exception is unhandled when the displayed message in the rescue clause "proves" it was rescued ?
I noticed that if I comment out the DBCONN.close statement, I only get the "Already in database" message, no more unhandled exception.
If however, I run the following program :
require "sqlite3"
DBCONN = DB.connect "sqlite3://%3Amemory%3A"
begin
DBCONN.exec "create table test ( id integer primary key, code text not null unique)"
DBCONN.exec "insert into test(code) values('A')"
sql = "select 1 from test where code = 'Z'"
code = DBCONN.query_one sql, as: {String}
rescue e
puts (e.message)
ensure
DBCONN.close
end
the output is a bare
no rows
So, no more unhandled exception and I'm somewhat puzzled : can you enlighten me on this?
Are there different "levels" of exceptions ?
I also noticed that putting the DBCONN.close in an at_exit statement produce the same "unhandled exception" for the first code snippet.
Thanks
The text was updated successfully, but these errors were encountered:
Hi,
I am trying to understand how exceptions are handled in Crystal-sqlite3.
Given the following code :
output is :
Why does Crystal tell me the exception is unhandled when the displayed message in the rescue clause "proves" it was rescued ?
I noticed that if I comment out the DBCONN.close statement, I only get the "Already in database" message, no more unhandled exception.
If however, I run the following program :
the output is a bare
So, no more unhandled exception and I'm somewhat puzzled : can you enlighten me on this?
Are there different "levels" of exceptions ?
I also noticed that putting the DBCONN.close in an at_exit statement produce the same "unhandled exception" for the first code snippet.
Thanks
The text was updated successfully, but these errors were encountered: