Exforsys.com
 
Home Tutorials J2EE
 

Why It is Important To Focus On Java Exceptions For Your Programs

 

Why It is Important To Focus On Java Exceptions For Your Programs

If you work with Java, it is important to understand exceptions. Exceptions are uncanny situations that can destroy the normal behavior of a program.



They are common in the Java programming language, and it is something you will have to deal with if you want to write good applications. Exceptions are defined as unchecked or checked, and they are also defined as being objects. Exceptions which are checked will stretch java.lang.Exception, and they will need to be controlled with a try/catch block. It is also possible to control them with a throws clause.

An exception which is unchecked will stretch java.lang.RuntimeException. They will not be reviewed by a compiler. While the checked exceptions can be useful, they may have a negative side effect, which is known as the "try/catch/do nothing" code. You can see an example of this here:


try
{
// Some code that generates checked exceptions
}
catch(Exception e)
{
// do nothing
}


Many documents have revealed information about the problems that this code causes. It is best for programmers to avoid it if possible. There are a large number of Java programs which contain this code. One reason for this is because many programmers don't know how to deal with exceptions. Many Java APIs will create multiple exceptions, and it is not always easy to handle them. The best way to deal with this situation is to give the task of dealing with exceptions to other portions of the program. You can also get rid of exceptions that you don't want to deal with. However, this may not always solve the problem. There is a simple method you can use to deal with exceptions.

Being able to properly handle exceptions will allow you to work on the parts of your program which are important. If you are building a Java program that will connect to a database, you will probably use the Java database connectivity API. You will always want to get a database connection element first. The code that you will need to use in order to obtain a connection object will be added within a single method. Below is an example of this code:

Listing 1: How a Database Connection Is Usually Retrieved


public static Connection getConnection() throws ClassNotFoundException, SQLException
{
Connection connection = null;

// Load the JDBC driver for your database
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);

// Create a connection to the database, usually those values are retrieved from a properties file
String serverName = "127.0.0.1";
String portNumber = "1521";
String sid = "mydatabase";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "username";
String password = "password";

connection = DriverManager.getConnection(url, username, password);

return connection;
}


The primary problem with using this method is that you will have to deal with java.lang.ClassNotFoundException, along with java.sql.SQLException. There is little you can do with these exceptions other than throw them. Both of these exceptions will keep a database connection for being reclaimed. Using a newer version of getConnection can allow you to avod having to deal with these exceptions.

At the same time, a runtime behavior may occur, and this is something you probably don't want. The exceptions in the above example should not be present once you've completed your application. If these exceptions are present while you design the application, it will not be able to correct them.

If it causes problems with a network, you will need the help of an adminstrator. If it is any other issue, you will need help from a programmer who is skilled with Java. In this situation, trying to write code to handle the exceptions is generally useless.



Read Next: Important Features of Java - Multithreading, AWT



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape