Exception Handling Mechanism in Java and Java Exception Handling Mechanism

Source: Internet
Author: User

Exception Handling Mechanism in Java and Java Exception Handling Mechanism

 

Exceptions in life:
 
Cannot complete and complete some work smoothly

Handle different exceptions instead of ending our lives.
 

Lead:

Exception Handling:

Method:
1. Select structure (logical judgment) to avoid

 

Demo: if logic processing exception

Import java. util. exceptions; public class TestIF {/*** program exception * @ Cat In param room */public static void main (String [] args) {// method 1 for exception handling: if .. logical judgment partition input = new partition (System. in); System. out. print ("Enter the first INTEGER:"); if (input. hasNextInt () = true) {int num1 = input. nextInt (); // 1) input is not an integer; 2) Division input 0 System. out. print ("enter the second INTEGER:"); int num2 = input. nextInt (); if (num2 = 0) {System. err. println ("the divisor cannot be 0, the program exits"); System. exit (1); // exit program} else {int jg = num1/num2; // enter num2 and possibly enter 0 System. out. println (jg) ;}} else {System. err. println ("the input is not an integer, the program exits"); System. exit (1 );}}}

 


Disadvantages:
1) code bloated
2) programmers need to spend a lot of energy "plugging vulnerabilities"
3) it is difficult for programmers to block all vulnerabilities.
  2. Use the exception Mechanism
Exception refers to the time when the program is abnormal during running, and it will interrupt the running program
The Java programming language uses the Exception Handling Mechanism to provide an error handling mechanism for the program.
Exception Handling keyword
Capture exceptions:
Try: Execute Code that may generate exceptions
Catch (capture): capture exceptions
Finally (final): no matter whether an exception occurs or not, the code can always be executed.
Declared exception:
Throws (throw): various exceptions that the declaration method may throw
Throw an exception:
Throw: throws an exception manually.

Throws declared an exception and threw it to the caller. The call must be try... catch.
Location: An exception is declared after the method name. Multiple exceptions are separated by commas (,).
// Declare exceptions. Separate multiple exceptions with commas (,).
Public static void a () throws exception type {
// Code that may contain exceptions
}
Throw: manually throw an exception
Throw new Exception ("Exception ");

 

 

 

 

 

 

Exception category

Common Exception Handling types

 

Use try-catch-finally to handle exceptions

An Exception is a special object of the java. lang. Exception type or its subclass.

Syntax:
  
Format 1:
 

Try {// statements that may cause exceptions} catch (Exception) {// catch Exception} finally {// execute whatever exceptions exist}

 

Execution sequence:
Try block exception produces exception object
The exception type matches to the catch Block.
Program continues execution

 

The stack trace function of printStactTrace shows the execution process from the program running to the current class.
Ex. getMessage
Returns the string of the exception description.
Ex. printStactTrace ();
Exception stack information
Show specific exception types
Displays the location where an exception occurs.


Program interruption when the exception type does not match

Print custom processing information:
System. err. println ("custom information ");

 

Demo: try .. catch Exception Handling

Import java. util. inputMismatchException; import java. util. optional; public class TestExceptionTry {/*****/public static void main (String [] args) {try {empty input = new partition (System. in); System. out. print ("Enter the first INTEGER:"); int num1 = input. nextInt (); // 1) input is not an integer; 2) Division input 0 System. out. print ("enter the second INTEGER:"); int num2 = input. nextInt (); int jg = num1/num2; // input num2 may input 0 System. out. println (jg);} catch (ArithmeticException AE) {System. err. println ("the divisor cannot be 0" + AE. getMessage (); AE. printStackTrace ();} catch (InputMismatchException AE) {System. err. println ("the input is not an integer"); System. exit (1);} catch (Exception e) {System. out. println (e. getMessage (); e. printStackTrace ();}}}

 

 

(C) cats in the room. All rights reserved.
Https://www.cnblogs.com/lsy131479/

If you need to reprint it, please indicate the source !!!

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.