Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Exception Classes 
 

The hierarchy of exception classes commence from Throwable class which is the base class in java.lang. This class can be instantiated and thrown by the program.

 

Exception Classes

                         

The hierarchy of exception classes commence from Throwable class which is the base class for an entire family of exception classes, declared in  java.lang package as java.lang.Throwable. A throwable contains a snapshot of the execution stack at the time it was created and also a message string that gives more information about the error. This class can be instantiated and thrown by the program. The throwable class is further divided into two subclasses :- 

  1. Exceptions - Exceptions are thrown if any kind of unusual condition occurs that can be caught. Sometimes it also happens that the exception could not be caught and the program may get terminated. Remember that they are a member of Exception family and can be type of Checked or Unchecked exception.
  2. Errors - When any kind of serious problem occurs which could not be handled easily like OutOfMemoryError then an error is thrown. Well, errors are not something which is thrown by you rather they are thrown by the Java API or by the Java virtual machine itself i.e. only the exceptions are thrown by your code and not the errors. Also Remember that they are a member of Error family.

The exception classes can be explained as well seeing the exception hierarchy structure:

 

 

The java.lang package defines several classes and exceptions. Some of these classes are not checked while some other classes are checked.

EXCEPTIONS DESCRIPTION CHECKED UNCHECKED
ArithmeticException Arithmetic errors such as a divide by zero

-

YES
ArrayIndexOutOfBoundsException Arrays index is not within array.length - YES
ClassNotFoundException Related Class not found YES -
IOException InputOuput field not found YES -
IllegalArgumentException Illegal argument when calling a method - YES
InterruptedException One thread has been interrupted by another thread YES -
NoSuchMethodException Nonexistent method YES -
NullPointerException Invalid use of null reference - YES
NumberFormatException Invalid string for conversion to number - YES

As you have come to know that exceptions are Objects that means an object is thrown when you throw an exception. Moreover only those objects could be thrown whose classes are derived from Throwable

It is interesting to note here that the objects of your own design could also be thrown provided that they should be the subclass of some member of the Throwable family. Also the throwable classes which are defined by you must extend Exception class. 

It depends upon the situation that whether to use an existing exception class from java.lang or create any of your own. Such as IllegalArgumentException, a subclass of RuntimeException in java.lang can be thrown if any method with an invalid argument is thrown by you. On the other hand you need not to worry if you wish to impart some more information about any unusual condition other than a class from java.lang because it will be indicated by the class of exception object itself.  

For example, if a thrown exception object has class IllegalArgumentException, that indicates someone passed an illegal argument to a method. Sometimes you will want to indicate that a method encountered an abnormal condition that isn't represented by a class in the Throwable family of java.lang.
For instance, lets tweak an example below that demonstrates the exceptional conditions that might occur while driving a car.

// In Source Packet in file except/ex1/SpeedException.java
class SpeedException extends Exception {
}
// In Source Packet in file except/ex1/VeryFastException.java
class VeryFastException extends SpeedException {
}
// In Source Packet in file except/ex1/VerySlowException.java
class VerySlowException extends SpeedException {
}

Lets tweak the diagram below.

It is clear from the above program that there is something abnormal with the speed of the car i.e. either it is very fast or it is very slow. Hence two exceptions are thrown by the program - VeryFastException and VerySlowException. To be more precise the SpeedException family specifies three new exceptions thrown by the program which indicate some abnormal conditions. That is the SpeedException specifies that there is something unusual with the speed; VeryFastException and VerySlowException specifies the abnormal conditions of the speed.

NOTE: The SpeedException extends Exception only and not the Throwable or Error class.

                         

» View all related tutorials
Related Tags: c exception orm event io struct exec uri int exe condition call interrupt program execution ram ical flow pic exceptions

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:


Respected Sir/Madam,
I hava applied SCJP os i need the sample questions and answer because i need to work out the java ,Servlet and JSP so please help me.
thanking you
regards
Viswanathan

Posted by viswanathan on Thursday, 02.7.08 @ 11:19am | #47486

man i should compliment you,cause this tutorial is of the hook.is it not possible for us to be able to download the tutorial from your site?
i am a student at the polytechnic of namibia studying IT and as part of my course i have to do Java(OOP).

Posted by rodger on Thursday, 10.18.07 @ 18:43pm | #34339

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.