Lecture 5 - Exception Handling, JDBC, Database connection
Lecture 5 - Exception Handling, JDBC, Database connection
Senior-lecturer
[email protected]
Astana IT University
▪ Exception Handling
▪ Try-catch and throw
▪ JDBC
▪ Database connection (PostgreSQL)
▪ An exception (or exceptional event) is a problem that arises during the execution
of a program
▪ Normal flow of the program is disrupted, and the program terminates abnormally,
which is not recommended, therefore, these exceptions are to be handled
▪ Exception handling enables applications to resolve (or handle) exceptions
▪ A program can continue executing as if no problem had been encountered
▪ Robust and fault-tolerant programs
▪ Graceful termination
▪ try-catch blocks are used to handle exceptions
▪ A code which might throw an exception is written inside
try block
▪ System-generated exceptions are automatically thrown
by the Java run-time system (e.g., dividing by zero)
▪ finally block contains the code that absolutely must be
executed after a try block
▪ It is possible to use several catch blocks defining an
exact Exception form
▪ *It is considered as “bad practice” to catch Errors
▪ Use throw keyword to manually throw an exception
▪ If a method throws a checked exception (and does not catch it), then it must declare
the fact in a throws clause
▪ Example:
In this example only one user was
added to group, since users[1]
was null and users[2] has the
same name as users[0].