Error:
Errors are the mistakes that make the program go
wrong.
An error may produce an incorrect output or may
terminate the execution of the program abruptly or even may cause the system to
crash.
Errors are classified into two categories:
o Compile Time
Errors: All errors will be detected
and displayed by the Java compiler and therefore these errors are known as
compile-time errors. Whenever a compiler displays an error, it will not create
the .class file.
The
common compile time errors are:
§ Missing semicolons.
§ Missing (or mismatch of) brackets in classes and
methods.
§ Misspelling of identifiers and keywords.
§ Use of undeclared variables and many more.
o Run Time Errors:Sometimes a program may compile properly and producing
.class files but May not run
properly. Most common run-time errors are:
§ Divide an Integer by zero.
§ Accessing an element that is out of the bounds of an
array.
§ Converting invalid string to a number.
§ Attempting to use a negative size for an array and many
more.
Exception
Types:
An Exception is a condition that is caused by a
run-time error in the program.
Exceptions in Java can be categorized into two
types:
o Checked
Exception: These exceptions are
explicitly handled in the code itself with the help of try catch blocks.
Checked exceptions are extended from the java.lang.Exceptionclass.
o Unchecked
Exceptions:These exceptions are
not essentially handled in the code; instead the JVM handles such exceptions.
Unchecked exceptions are extended from the java.lang.RuntimeExceptionclass.
Some common Java Exceptions are:
Exception Type
|
Cause of Exception
|
ArithmeticException
|
Caused by math errors such as division
by zero.
|
ArrayIndexOutOfBoundsException
|
Caused by bad array indexes.
|
ArrayStoreException
|
Caused when a program tries to store
the wrong type of data in an array.
|
FileNotFoundException
|
Caused by an attempt to access a
nonexistent file.
|
IOException
|
Caused by general I/O failures, such
as inability to read from a file.
|
NullPointerException
|
Caused by referencing a null object.
|
NumberFormatException
|
Caused when the conversion between
strings and numbers fails.
|
OutOfMemoryException
|
Caused when there’s not enough memory
to allocate a new object.
|
SecurityException
|
Caused when an applet tries to perform
an action not allowed by the browser’s security setting.
|
StackOverFlowException
|
Caused when the system runs out of
stack space.
|
StringIndexOutOfBoundsException
|
Caused when a program attempts to
access a nonexistent character position in a string.
|
Exception Handling:The purpose of exception handling mechanism is to
provide a means to detect and report an “exceptional circumstance” so that
appropriate action can be taken. The following tasks must be performed during
exception handling:
o Find the problem(Hit the exception)
o Inform that an error has occurred(Throw the
exception)
o Receive the error information(Catch the exception)
No comments:
Write comments