Java Exception Descriptions

Java Exception Descriptions thumbnail
Java exceptions help when handling errors.

Exceptions in Java are thrown for various reasons. In general, an exception is caused by something that is not part of the normal execution of a program. Having an understanding of the different types of exception in Java helps when you attempt to handle them. A few exceptions comprise the most common varieties that beginners are likely to encounter, and learning to cope with these equips you with the skills for exception-handling generally.

  1. Class Cast Exceptions

    • Class Cast Exceptions are thrown when a program tries to cast objects inappropriately. In Java, you can cast an object from one class to another as long as the two classes exist within the same hierarchy, i.e., where the new type is either a subclass or superclass of the object's existing class. If you try to perform a casting operation that is not valid because the two classes concerned are not within the same hierarchy, a Class Cast Exception will be thrown.

    Number Format Exceptions

    • Among the most common types of exception extending the Illegal Argument Exception class, Number Format Exceptions occur when Java code tries to convert a text string to a particular number type but the string does not contain appropriate characters for the number type in question. Converting from a string to a number is a common task in Java, particularly in cases where the string has been received as a parameter, for example a command line argument. Converting a string to a number type will cause a Number Format Exception if the string does not contain a number in the correct format.

    Input Output Exceptions

    • Input Output Exceptions, or "IOExceptions" in Java are caused when something goes wrong when reading from or writing to some external resource. There are many types of I/O exceptions in Java, but the type IOException is the main superclass for all exceptions caused by these operations. An example of an IOException would be one caused by attempting to write to or read from a file that does not exist.

    Null Pointer Exceptions

    • Java Null Pointer Exceptions are thrown when some part of program execution attempts to access an object but instead encounters "null." Java object references point to the location in memory where the data for the object is stored, but if the object does not exist, for example because it has not been instantiated, the reference will instead point to null. If the program attempts to carry out an operation on a null object, such as calling a method on it or reading one of its fields, a Null Pointer Exception will be thrown.

    Index Out Of Bounds Exceptions

    • Index Out Of Bounds Exceptions are commonly encountered when using strings and arrays, in which case they typically take the form of subclasses, Array Index Out Of Bounds and String Index Out Of Bounds. If you attempt to access an index that is out of the range of a particular array or string, an Index Out Of Bounds Exception will be thrown. For example, if you create and instantiate an array with length 12, and then attempt to read from index 15, an exception will be thrown.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/Photos.com/Getty Images

Comments

You May Also Like

  • How to Create a Java File

    Creating and writing data to a file in Java can be a slightly counter-intuitive task since the creation of the built-in Java...

  • How to Write My Own Exception Handler in Java

    The Java language provides the exception model so developers can handle situations in which normal program execution goes wrong. There are many...

  • Java Exception Handling Tutorials

    Java Exception Handling Tutorials. Writing applications in Java allows you to make easy and effective use of different resources. The Java language...

  • How to Create User-Defined Exceptions in Java

    Create the function's shell. A user-defined exception is an extension of the internal exception class. In this example, the exception is set...

  • File Handling in Java Tutorial

    One of the features of the Java programming language is the large library of standard classes for solving routine programming tasks with...

  • How to Catch an Exception in a Static Block in Java

    Enter code to access the array defined in Step 3 at a position that is not defined. This code will then result...

  • How to Convert Tire Size to Height

    Tire sizes can be confusing to the consumer since the information molded into the side of the tire seems to be a...

  • How to Create a Custom Exception in Java

    Exceptions are a key tool in helping Java programs cope with the unexpected. As a developer, you can use the standard exception...

  • How to Declare Exception Errors in Java

    Errors can occur from several sources when the computer utilizes applications. These sources are either a programming error or errors that arise...

  • How to Convert Java Objects

    The Java programming language was unique when it was first released in the mid 1990s in that the language was designed to...

  • Java Exceptions Tutorial

    An exception occurs when an abnormal situation (such as an error or problem) occurs that a method is unable to handle. The...

  • Java & Unresolved Compilation Error

    In Java, programmers write source code to be compiled into a Java bytecode file. This file will be readable by any computer...

  • Cause of Java Exceptions

    Java is like all other programming languages in that there are strict rules on how data is handled and logic is followed....

  • What Is VAT Input?

    Value Added Tax is levied by countries within the European Union on purchases of goods and services. Each country fixes the VAT...

  • How to Typecast in Java 6

    In computer programming, typecasting refers to taking an object of one type and converting it into an object of another type. In...

Related Ads

Featured