Java provides support for both checked and unchecked exceptions. The main differences between these exceptions is summarised below.
Checked Exception
extends Exception
caller must handle exception
e.g. FileNotFoundException
Unchecked Exception
extends RuntimeException
caller does not have to handle exception
e.g. NullPointerException
So, when should you use checked exceptions and when should you use unchecked exceptions?
A good guidline is to throw checked exceptions when the caller can reasonable handle a failure and can perform appropriate action. For example a client could easily perform some corrective action if a FileNotFound exception were to be thrown. If however the system throws a ClassCastException (which is an unchecked exception), there's not much that the caller could do about this. In this case, it may be better to throw a unchecked exception.
In J2EE containers, unchecked exceptions are caught by the container, and would, for example, cause an EJB transaction to be rolled back.
In Spring, unchecked exceptions are used extensively for data access code in the form of DataAccessExceptions. Typically if an error occurs in data access code, the client application cannot recover and a roll-back operation is performed. In this case unchecked exceptions (e.g. DataAccessException) are appropriate.
World of tricky Core Java Q and A Covering Object Oriented Analysis and Design, JVM Internals,Java Language Fundamentals,Datatypes,Keywords,Operators and Assignments,Identifies,Declarations and Modifiers, Conversion,Casting and Promotion, Flow control,Assertions, Exception Handling and Garbage Collection,Objects and Classes), Basic Packages and their classes,JDBC,JFC Swing,Java Server Pages (JSP), Servlets,EJB,JMS,JNDI etc and Open source technologies like Struts,Hibernate,Spring etc
Search This Blog
Friday, July 07, 2006
Wednesday, July 05, 2006
If a class is located in a package, what do you need to change in the OS environment to be able to use it?
You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let's say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:
c:\>java com.xyz.hr.Employee
c:\>java com.xyz.hr.Employee
What is a task's priority and how is it used in scheduling?
A task's priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.
Subscribe to:
Posts (Atom)