How you handle the exception would depend on what you need the database for. If the database is an important part of your program and you can not execute without it, you might want to display an error message to the user and than exit. Or maybe you want to try to connect to a secondary database in case you can not connect to your main db.
On the other hand, if your database contains optional data, maybe it is fine for you program to execute without a db connected. In that case you would return and store your database in variable with type Database?
As to which exception to throw, this is often a hard question. I normally dislike to create a new Exception types for every case as it pollutes the codebase and is not really helpful, except if you want to later differentiate between to exceptions, but I normally stick to the default Java and Kotlin exceptions. In your case I would probably throw an IOException, because a databaseās job is to āinputā and āoutputā data (well yeah save data, same thing )
PS: I might be wrong there but in the part where you get catch part where you get the database also needs to throw an exception or return null (whatever you choose to do in case of an error )
Yeah, knowing and using the standard library functions require and check is really useful. @thebalirequire is for checking the validity of passed arguments, and check for checking the state, e.g a variable in your class.
For the case ābad db nameā I would also use require as @fvasco suggested even though it is technically not an argument to the function.
I have written āreturn nullā instead of āreturnā. Still no progress.
it says, null cannot be a value of non-null type Database
I want to stop the execution of all program and return some exception here. Also, I have another question, does throwing an exception means also halting the execution of program, or do we have to do it delibrately?
The return value of your method is Database. Normally a variable or return value or any place where you have a type, you can not use null. If you want a type to be nullable you need to write a ? behind the type name to tell Kotlin that this is a nullable type. So your return type needs to be Database?.
You only set it inside of a try block, which is may not execute, because it could fail. Therefor it is not always initialized, which leads to your error.
The function above is the final function I gathered from your knowledge. Please suggest any best practices I could use here which will result in efficient code.