Try catch block-------------exception handling in Java (2)

Source: Internet
Author: User
Tags finally block try catch

1. What is a try block?

A try block is a block of code that can produce an exception, and a try block may follow a catch block or a finally block, or both.

The semantics of the try block:

try{   //statements that could cause an exception}

2. What is a catch block?

A catch block is associated with a try block, and if a specific type of exception occurs in a try block, the response catch block executes, for example,

If arithmmetic exception occurs in a try block, the catch block corresponding to arithmmetic exception occurs.

The semantics of the Catch block:

try{     //statements that could cause an exception}catch (exception (type) E (object)) {     //error handling code}

3. Execution flow of Try-catch block

If an exception in a try block occurs, the execution control is passed to the catch block, the exception is caught by the corresponding catch block, and a

A try block can have multiple catch blocks, but each catch block can only define an exception class.

When all the code in the try block finishes executing, the code in the finally block executes. Containing a finally block is not mandatory,

But if you have a finally block, it will run, whether or not an exception is cut out or processed.

An example of a try-catch:

Class Example1 {public   static void Main (String args[]) {     int num1, num2;     try {         //try block to handle code, cause exception        num1 = 0;        num2 = 62/NUM1;        System.out.println ("Try block Message");     } catch (ArithmeticException e) {             //This block was to catch Divide-by-zero error            System.out.println ("Error:don ' t di Vide a number by zero ");       }     System.out.println ("I ' m out of the Try-catch block in Java.");}   }

Output:


I ' m out of Try-catch block in Java.

4. Multiple blocks of code in Java

A try block can have multiple catch blocks

A catch block that captures the exception class can catch other exceptions

catch (Exception e) {//this Catch block catches all the exceptions}

If more than one catch block exists, the catch mentioned above should be put to the last.

If the try block does not throw an exception, the catch block is ignored and the program continues.

If the try block throws an exception, the corresponding catch block will handle it.

The code in the Catch block executes, and the program continues execution.

Class example2{public   static void Main (String args[]) {     try{         int a[]=new int[7];         a[4]=30/0;         System.out.println ("First print statement in try block");     }     catch (ArithmeticException e) {        System.out.println ("warning:arithmeticexception");     }     catch (ArrayIndexOutOfBoundsException e) {        System.out.println ("warning:arrayindexoutofboundsexception");     }     catch (Exception e) {        System.out.println ("Warning:some other Exception");     }   System.out.println ("Out of Try-catch block ...");}  
Output:
Warning:arithmeticexceptionout of Try-catch block ...


Try catch block-------------exception handling in Java (2)

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.