Java Exception Handling Tutorial-2

Advanced Exception Handling in Java Tutorial with Example Programs

This post is in continuation of the previous Java Exception Handling Tutorial. After reading the previous post, the basic concept of using try and catch block should be clear. The other thing that is to be noted in context of using try and catch block is that try block can not exist alone, it must be followed by at least one catch block or finally block. The next step that you should learn about is the nesting of try blocks.

Nesting Try Blocks

The try statement can be nested. That is, a try statement can be inside the block of another try. Each time a try statement is entered, the context of that exception is pushed on the stack. If an inner try statement does not have a catch handler for a particular exception, the stack is unwound and the next try statement's catch handlers are inspected for a match. This continues until one of the catch statements succeeds, or until all of the nested try statements are exhausted. If no catch statement matches, then the Java run-time system will handle the exception.
Below example program demonstates how the nesting of try blocks work in Java exception handling.

Program to demonstate nesting of try blocks in Java Exception Handling

Java Exception Handling Tutorial

Exception Handling in Java Tutorial with Example Programs

Exceptions in Java - What are Java Exceptions?

A Java Exception is an abnormal condition that arises during the execution of a program and also called a run-time error. An exception in Java signals the unusual or catastrophic situations that can arise.
Examples of Java Exceptions are:
  • Incorrect user input such as division by zero
  • File that needs to be opened not found
  • Network connection problem

Types of Java Exceptions

Like anything else in this world, Exceptions can also be categorized! Based on whether or not you are required to handle the exception, there are two types of exceptions: Checked and Unchecked Exceptions.
Java Exception Class Heirarcy
Exception Class Heirarchy
Checked Exceptions: These are the type of exceptions for which the compiler checks to ensure that your code is prepared for handling such exceptions. These are the exceptions that you can expect to occur frequently and you must handle them in your code. The conditions that generate such exceptions are generally outside the control of your program and they can occur in a correct program. But you can anticipate them and thus you must write code to deal with them. Programmatically, checked exceptions are the instances of the Exception class or one of its subclasses, excluding RuntimeException subtree.

Unchecked Exceptions: The compiler does not check for such type of exceptions. Unchecked Exceptions comprise of  run time exceptions (of type RuntimeException or its subclasses) and errors (of type Error or its subclasses). Runtime Exceptions occur due to program bugs and include exceptions such as division by zero and invalid array indexing. You can not recover from an unchecked exception and you are not required to handle such type of exceptions either, but still you can do so if you want. ArithmeticException class is an example of unchecked exception. Errors are the exceptions that are not expected to be caught under normal circumstances by your program. They are used by the Java run time environment to indicate errors related to run time environment itself. Stack Overflow is an example of error.

Core Java Project Desktop Application : Cab Invoice Generator

Invoice Generator Core Java Desktop Application Using Swing

Description

Invoice Generator is a Core Java Project for generating the invoices for a Rent-a-Cab company. It takes the details of the invoices via a form and then calculates the various subtotals, service taxes, rates applicable etc on the basis of choices entered and then generates the invoice for the user. 
It also allows you to Add, Edit or Remove the cabs, define and modify the various rent packages for customers, billing schemes etc. The company owner can change each and every detail about how the invoice will be calculated and generated.
I have used the MS-Access database for simplicity as this project is intentionally for the beginners in java programming. If you want to run the project, you must have to create the DSN with the name "invoice" and point it to the database that is provided with this project. Alternatively you can create the DSN with any name but in that case you will have to change the DSN name in each occurence of DSN in the project.
I have left out the various checks for type of data that can be entered in the fields and other validation checks in almost every forms. So you can download the project source code and do it yourself.
I will also additional functionalities like PRINT the invoice to this project in a week or two as and when I will get time. But for now it is a good beginner level Core Java Project for you to look at and understand the basic concepts and try some modification of your own. Like I have built it for a Rent-a-Cab company but you can modify it to accomodate the needs of some other firm.

JDBC : Connection to databases

What is JDBC?

JDBC is an API that encapsulates the low−level calls needed for database access and interaction into one common interface. Both the Java Development Kit (JDK) and Java Runtime Environment (JRE) contain the API as part of the standard distribution. The API’s interfaces and classes reside in the java.sql and javax.sql packages. The standard components are packaged in java.sql while the enterprise elements are in javax.sql.

Steps for building a simple JDBC application

1. Import Packages
2. Register JDBC Driver
3. Open a Connection
4. Execute a Query
5. Extract data from Result Set
6. Clean-Up Environment

A Simple Program to demonstrate the JDBC application working:


New Core Java Projects Added

I have collected some of the best Core Java Projects for you to work upon.
There are six Core Java Projects synopsis cum project report that will help you develop the project. All the projects can be developed in Java SE.  You may use any IDE of your choice but I would prefer Netbeans 7 as I find it more comfortable and easy to use even though it eats up more memory!
I have studied all the project reports in detail but I haven't developed them all. I have only collected them from the different sources and have put them together for your convenience.
If you have any difficulties in developing on the following given projects, you may contact me.
Your feedback is important so do join this blog and forget not to write in your ideas, suggestions and problems as comments.
Core Java Projects (Synopsis)