Java exception mechanism

Source: Internet
Author: User
Tags class definition

Java exception mechanism

   software programs in the process, it is very likely to encounter these issues just mentioned, we call the exception, English is: Exception. In the Java exception handling mechanism, a lot of classes are introduced to describe and handle exceptions, called exception classes. The exception class definition contains information about the class exception and methods for handling the exception. The so-called exception handling, refers to the program in the event of a problem can still be done correctly.
Java uses an object-oriented approach to handling exceptions. Processing process:
1. Throw an exception: When executing a method, if an exception occurs, the method generates an object representing the exception, stops the current execution path, and submits the exception object to the JRE.
2. Catch exception: After the JRE gets the exception, look for the appropriate code to handle the exception. The JRE looks in the method's call stack, starting with the method that generated the exception, until the appropriate exception-handling code is found.
Common Exceptions:
1:arithmeticexception Math Anomaly, was 0 apart
2:inputmismatchexception input type mismatch exception
3:java.lang.arrayindexoutofboundsexception: Array subscript out-of-bounds exception.
4:java.io.ioexception:javaio exception.
5:java.lang.nullpointerexception: null pointer exception, using an object that has not been initialized.
Classification of exceptions:

Error
error is a bug that the program cannot handle, indicating a more serious problem in running the application. Most errors are unrelated to what the code writer does, and represent a problem with the JVM (the Java virtual machine) that is running the code.

Exception
Exception are exceptions that the program itself can handle, such as null pointer exceptions (nullpointerexception), array subscript out-of-bounds exceptions (arrayindexoutofboundsexception), type conversion exceptions ( classcastexception), arithmetic anomalies (arithmeticexception), and so on.
The exception class is the parent class for all exception classes, and its subclasses correspond to a variety of possible exception events: Java Exceptions can be divided into:
1. RuntimeException Run-time exception
2. Checkedexception has checked for exceptions
RuntimeException Run-time exception
Such exceptions are usually caused by programming errors, so when writing a program, it is not necessary to use exception handling mechanisms to handle such exceptions, often by adding "logical processing to avoid these exceptions".
Null pointer exception:

To resolve a null pointer exception:

Array angle label out of bounds exception:

checkedexception checked for exceptions
All exceptions that are not runtimeexception, collectively referred to as checked Exception, are also known as "checked exceptions", such as IOException, SqlException, and user-defined Exception exceptions. Such exceptions must be processed at compile time, or they cannot be compiled.

Exception handling: There are two common ways of handling:use "Try/catch" to catch exceptions, and "throws" to declare exceptions.
One way to handle exceptions: catching exceptions
The catch exception is achieved by 3 keywords: try-catch-finally. Use a try to execute a program, if an exception occurs, the system throws an exception, it can be caught by its type (catch) and processing it, the final step is to provide a uniform exit through the finally statement for exception handling, finally the code specified by the execution ( A catch statement can have more than one, and a finally statement can have up to a single line, which is optional for your own needs.
Note the point:
1, a try statement must have at least one catch statement block or a finally statement block, and when the exception-handling code finishes executing, it will not go back to the try statement to execute the code that has not yet been executed.
2. Each try statement block can accompany one or more catch statements to handle the different types of exception objects that may be produced.
3, regardless of whether an exception occurred, even if there is a return statement in the try and catch block, finally must be executed
4, finally statement block only one situation is not performed, that is, before the implementation of finally encountered System.exit (0) to end the program run.
Exception handling two: declaring exceptions (throws clauses)
When the checkedexception is produced, it is not necessarily dealt with immediately, and the anomaly can be throws out again. If a method throws multiple checked exceptions, all exceptions must be listed at the header of the method, separated by commas.
Throw
1: Can only be used in the method body.
2: followed by the exception object.
3: Can only be followed by an exception object
Throws
1: Used at the declaration of the method. The argument list is preceded by the method body.
2: The type followed by the exception.
3: Can be followed by multiple exception types. Separate with commas.

Java exception mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.