Five keywords for Java Exception Handling and java Exception Handling

Source: Internet
Author: User
Tags finally block

Five keywords for Java Exception Handling and java Exception Handling

Exception: some are caused by user errors, some are caused by program errors, and others are caused by physical errors.

Keyword for exception handling: try, catch, finally, throw, throws

Note:

  1. The error is not an exception, but an issue that is out of programmer control.
  2. All Exception classes are subclasses inherited from the java. lang. Exception class.
  3. The exception class has two main subclasses: The IOException class and the RuntimeException class.
  4. Java has many built-in exception classes.

Exception classification:

  1. The user entered invalid data.
  2. The file to be opened does not exist.
  3. The connection is interrupted during network communication, or the JVM memory overflows.

Syntax:

Try {// code block to be listened on}
Catch (exception type Exception name/e) {// handle the error code block caught by the try listening. throw Exception name/e; // thorw indicates throwing an exception throw new exception type ("Custom ");}

The statements in the finally {// finally block will be executed no matter whether the exception occurs or not.} The method name returned by the modifier () throws exception type {// throws is only used to declare the exception, whether to throw is determined by the method caller // code block}

Code example: (try and catch and finally)

Public class ExceptionTest {public static void main (String [] args) {custom input = new partition (System. in); try {// listening code block int a = input. nextInt (); int B = input. nextInt (); double sum = a/B; System. out. println (sum);} catch (InputMismatchException e) {System. out. println ("only numbers allowed");} catch (ArithmeticException e) {System. out. println ("the denominator cannot be 0");} catch (Exception e) {// Exception is the parent class System of all exceptions. out. println ("Other exceptions occurred");} finally {// whether or not an exception occurs, finally will be executed. out. println ("program ended ");}}}

Code example: (throw keyword)

Import java. util. inputMismatchException; import java. util. extends; public class ExceptionTest {public static void main (String [] args) {extends input = new evaluate (System. in); try {// listening code block int a = input. nextInt (); int B = input. nextInt (); double sum = a/B; System. out. println (sum);} catch (InputMismatchException e) {// catch (exception type Exception name) System. out. println ("only numbers allowed"); throw e; // throw the catch exception // throw new InputMismatchException (); same as} catch (ArithmeticException e) {System. out. println ("the denominator cannot be 0"); throw new ArithmeticException ("the denominator is 0 and an Exception is thrown"); // ArithmeticException thrown} catch (Exception e) {// Exception is the parent class System of all exceptions. out. println ("Other exceptions occurred");} finally {// whether or not an exception occurs, finally will be executed. out. println ("program ended ");}}}

Code example: (throws)

Public class Throws {int a = 1; int B = 0; public void out () throws ArithmeticException {// you can declare multiple exceptions that may be thrown, comma Separated try {// listening code block int sum = a/B; System. out. println (sum);} catch (ArithmeticException e) {System. out. println ("the denominator cannot be 0");} finally {// whether or not an exception occurs, finally will be executed System. out. println ("program ended") ;}} public static void main (String [] args) {Throws t = new Throws (); t. out (); // call method throw new ArithmeticException ("the denominator is 0 and an exception is thrown "); // determines whether to throw an exception based on the called method/** method 2 * // ArithmeticException a = new ArithmeticException ("the denominator is 0 and an exception is thrown "); // throw ;}}

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.