Java Learning Note (10) Exception handling

Source: Internet
Author: User

Exceptions are problems that occur during the execution of a program, resulting in an exception: the user enters invalid data, cannot find a file to open, the network connection is disconnected during communication, or the JVM has a memory overflow, and so on.

An exception is an object that is thrown by a method.

Exception classification:
Check for exceptions: Checking for exceptions is usually a user error or a problem that cannot be foreseen by the programmer.
Run-time exception: is a program type that can be avoided by programmers during the course of a program's operation.
Error: In fact, the error is not an exception at all, but it is an issue beyond the control of the user or programmer. Errors are often ignored when designing a Java application.

Three options for exception handling:
(1). Catch this exception and not let it continue to throw down the call stack;
(2). Catch this exception and continue to throw down;
(3). This exception is not caught, causing the method to be ejected from the call stack, and the exception object continues to be thrown to the main () method below the call stack.

Only objects of the Throwable type can be thrown by the JVM, its subclass exception is the parent of all exceptions, RuntimeException is a run-time exception, an exception is a subclass of exception, but not runtimeexception subclasses, This exception is a check exception, and the subclass error is the parent class for all errors.

Catch exceptions using Try/catch code blocks, for example:
try {
while (Rs.next ()) {
is = true;
Printing information
Print (RS);
}
if (!is) {
System.out.println ("You don't have a pet yet!");
}
} catch (SQLException e) {
E.printstacktrace ();
}

If there is no exception in the try, the catch block is ignored, and if there is an exception in the try, the code after the exception that is thrown in the try does not execute and jumps to the place where the exception was caught.

Catch exceptions when a special exception is caught before catching a normal exception.

The throws keyword declares an exception, and throw throws an exception.

Finally keyword, the finally keyword is always executed, regardless of whether the exception occurred.
try {
while (Rs.next ()) {
is = true;
Printing information
Print (RS);
}
if (!is) {
System.out.println ("You don't have a pet yet!");
}
} catch (SQLException e) {
E.printstacktrace ();
} finally {
Freeing resources
Jdbcutil.close ();
}

Java Learning Note (10) Exception handling

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.