Basic application of exceptions in Java (i)

Source: Internet
Author: User

In Java, we treat exceptions as an object, and it is the introduction of exception mechanisms that make our programs more robust. An exception indicates an unhealthy condition, or an error condition, simply an event that interrupts the normal flow of instruction. Program control throws unconditionally into a specific code snippet, where the exception is caught and handled, separating the error handling from the normal processing of the program, and the error can be handled separately.

Exceptions are represented by instances of the exception class and its subclasses, and most packages define their own exception types, which are often subclasses of exception, and we can define the type of the exception ourselves. When an error condition occurs, a piece of code should be used to create the exception object. The exception object is passed as a parameter to the code block responsible for processing, which involves throwing and capturing exceptions.

, which can be thrown in addition to exceptions and error classes. The error class usually indicates some significant errors or virtual machine errors that should not be captured by the program. Exceptions are divided into two categories, run-time exceptions and compile-time exceptions, or they can be called unchecked exceptions and checked exceptions. The IO exceptions and SQL exceptions we typically encounter are checked exceptions, and for such exceptions, the compiler requires that we have to handle them. The runtime exception can be taken over by a virtual machine instead of being processed.

When an exception occurs, it is either handled directly or thrown up, waiting for the upper call method to be processed. If it is not processed at all, then if it is multithreaded, it is thrown by Thread.run (), if it is a single thread thrown by main (), then the corresponding thread or the entire program exits. Runtime exception and error can be left out of the process, so when the program encounters such an error, it terminates the exit without processing the block program.

How to handle Exceptions:

1. try-catch-finally Structure Handling exception

Try {    // Normal code, contains code    that may be faulted // If there is a return, go first finally and return return}  Catch(Exception type Exception e) {    // exception handling     }finally{ // finally block, which runs regardless of whether the program appears to be an exception // Abnormal Exit }

2, temporarily do not handle, throw up

Throw is used to manually throw an exception (the system cannot recognize an exception), and throws is declared to describe the type of error that the method might be sent to, and try-catch processing when the method is called. If not declared, Try-catch processing is required within the method.

 Public void setage (intthrows  exception{    if(age>=15&&age< =40) {        this. age= age;    }  Else {        thrownew Exception ("age must be between 15 and 40! ");    }

This example makes Try-catch processing when calling the Setage method, or continues to throw up.

Basic application of exceptions in Java (i)

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.