<< 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

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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