<< Chapter < Page Chapter >> Page >
This module explains the various exceptions that are raised by the Java Runtime Environment.

Out of range

These exceptions are the most common and are caused by attempting to access an array or string with an index that is outside the limits of the array orstring. C programmers will be familiar with the difficult bugs that are caused by such errors which “smear” memory belonging to other variables; in Javathe errors cause an exception immediately when they occur thus facilitating debugging.

ArrayIndexOutOfRange

This exception is thrown when attempting the index an array a[i] where the index i is not within the values 0 and a.length-1 , inclusive. Zero-based arrays can be confusing; since the length of the array is larger than the last index, it is not unusual to write the following in a for -statement by mistake:

final static int SIZE = 10; int a = new int[SIZE];  for (int i = 0; i <= SIZE; i++)     // Error, <= should be <  for (int i = 0; i <= a.length; i++) // Better, but still an error

StringIndexOutOfRange

This exception is thrown by many methods of the class String if an index is not within the values 0 and s.length() , inclusive. Note that s.length() is valid and means the position just after the last character in the string.

NullPointerException

A variable in Java is either of a primitive type such as int or boolean or of a reference type : an array type, the type of a class defined in the Java API such as String , or the type of a class that you define such as MyClass . When declaring a variable of a reference type, you only get a variable that can hold a reference to an object of thatclass. At this point, attempting to access a field or method of the class will cause the exception NullPointerException to be thrown: The name of the exception is an anachronism, because there are no (explicit) pointers in Java.

class YourClass {   static MyClass my;    // Can hold a reference but doesn't yet    public static void main(String[] args) {     my.m(5);            // Throws NullPointerException  } }

Only after instantiating the class and executing the constructor do you get an object:

class YourClass {   static MyClass my;    // Can hold a reference but doesn't yet    public static void main(String[] args) {     my = new MyClass(); // Instantiates an object                        //   and assigns the reference     my.m(5);            // Throws NullPointerException  } }

This exception is not often seen in this context because Java style prefers that the declaration of the variable include the instantiation as itsinitial value:

class YourClass {   // Declaration + instantiation of class variable  static MyClass my1 = new MyClass();    public static void main(String[] args) {    my1.m(5);            // OK     // Declaration + instantiation of local variable    MyClass my2 = new MyClass();     my2.m(5);            // OK  } }

Sometimes, you cannot initialize a variable at its declaration; an obvious place to initialize an instance variable is within its constructor:

class YourClass {   MyClass[] my;          // Can't initialize here because ...    YourClass(int size) {  // ... size of array different for each     object my = new MyClass[size];   }}

The exception is likely to occur when you declare an array whose elements are of reference type. It is easy to forget that the array containsonly references and that each element must be separately initialized:

MyClass[] my = new MyClass[4];       // Array of references my[1].m(5);                          // Raises an exception  for (int i = 0; i < my.length; i++)   my[i] = new MyClass();             // Instantiate objects my[1].m(5);                          // OK

Finally, NullPointerException will occur if you get a reference as a return value from a method and don't know or don't check that the value isnon-null: You could know this if the method has been verified for a postcondition that specifies a non-null return value.

Node node = getNextNode(); if (node.key > this.key) ...         // Error if node is null!  if (node != null) {                  // This should be done first   if (node.key > this.key) ... }

Computational errors

The following three exceptions occur when you try to convert a string to a number and the form of the string does not match that of a number, for example "12a3" .

InputMismatchException

This exception is thrown by the class Scanner , which is a class introduced into version 5 of Java to simplify character-based input to programs.

IllegalFormatException

This exception is thrown by the method format in class String that enables output using format specifiers as in the C language.

NumberFormatException

This exception is thrown by methods declared in the numeric “wrapper” classes such as Integer and Double , in particular by the methods parseInt and parseDouble that convert from strings to the primitive numeric types.

ArithmeticException

This exception is thrown if you attempt to divide by zero.

Important note
Most computational errors do not result in the raising of an exception! Instead, the result is an artificialvalue called NaN , short for Not a Number . This value can even be printed:

double x = Math.sqrt(-1);  // Does not throw an exception! System.out.println(x);

Any attempt to perform further computation with NaN does not change the value. That is, if x is NaN then so is y after executing y = x+5 . It follows that an output value of NaN gives no information as to the statement that first produced it. You will have to set breakpoints or use printstatements to search for it.

Insufficient memory

Modern computers have very large memories so you are unlikely to encounter these exceptions in routine programming. Nevertheless, they can occur as a side effectof other mistakes.

outOfMemoryError

This exception can be thrown if you run out of memory:

int a = new int[100000000];

StackOverFlowArea

A stack is used to store the activation record of each method that is called; it contains the parameters and the local variables as well as other informationsuch as the return address. Unbounded recursion can cause the Java Virtual Machine to run out of space in the stack:

int factorial(int n) {   if (n == 0) return 1;  else return n * factorial(n + 1);  // Error, you meant n - 1 }

Questions & Answers

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Compile and runtime errors in java. OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10913/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Compile and runtime errors in java' conversation and receive update notifications?

Ask