Java: Exception Handling, java Exception Handling

Source: Internet
Author: User

Java: Exception Handling, java Exception Handling
Content:

  • Exception description
  • Exception Handling
  • Assertions

 

 

Start Date:

Exception:
  • An exception is an error that occurs during the running of the program. A common example is "Division by zero exception". If a division is zero, this exception occurs.
  • Exceptions will affect the normal operation of the program, so we need to handle exceptions.
  • All Exception classes are subclasses inherited from the java. lang. Exception class. The exception class has two main subclasses: The IOException class and the RuntimeException class.

 

Common exceptions:

Arithmetic exception class: ArithmeticExecption

Null Pointer exception class: NullPointerException

Type forced conversion exception: ClassCastException

Array subscript out of bounds exception: ArrayIndexOutOfBoundsException

Input and Output exceptions: IOException

 

Handling exception:

 

  • Exception Capture: try... Catch... Finally
    • Format:
    • Try code block: Contains code that may occur due to exceptions.
    • Catch code block [multiple]: capture exceptions
      • E is the caught exception class object. The result of a string (including the abnormal thread, type, and location) is printed directly)
      • E. getMessage (): returns only the exception type + cause
      • E. printStackTrace (): exception type + cause + location
      • E. toString (): exception type + cause
    • Finally [Optional]: Put the code that will be executed whether an exception occurs or not.
    • If the Exception class cannot be identified and captured accurately, it can be caught as the parent class Exception of all Exception classes.
    • When an exception is caught successfully, try... Catch... The code after the finally code block can be successfully executed.

 

 

Public class Demo {public static void main (String [] args) {// int a = 10/0; try {int a = 10/0;} catch (ArithmeticException e) {System. out. println ("run in ArithmeticException" + e); // run in ArithmeticException java. lang. arithmeticException:/by zero} catch (Exception e) {System. out. println (e);} finally {System. out. println ("final execution"); // final execution }}}

 

 

  • Exception statement:

Throws is used to declare exceptions, and declarations of possible exceptions of functions. [When a function throws an exception, the function header must use throws to declare the exception]

 

  • Throw an exception:

Throw is used to manually throw an exception and throw custom exception information: throw exception type (exception information)

 

Public class Demo2 {static int div (int a, int B) throws ArithmeticException {if (B = 0) {throw new ArithmeticException ("Zero division exception occurred! ");} Return a/B;} public static void main (String args []) {try {System. out. println (div (2, 0);} catch (ArithmeticException e) {System. out. println (e. getMessage (); // zero division exception! } Finally {System. out. println ("in finally"); // in finally} System. out. println ("after finally"); // after finally }}

 

 

Generally, throw throws are used when you do not want to handle exceptions in functions. Otherwise, try... Catch... Finally catch exceptions.

 

 

Custom exception:

Sometimes we do not define the exception we want (for example, the MYSQL connection exception), so we can customize the exception.

  • All exceptions must be a subclass of Throwable.
  • If you want to write a checking Exception class (which is an Exception that some compilers will help check), you need to inherit the Exception class.
  • If you want to write a runtime exception class (for example, an array subscript out of bounds or an access NULL pointer exception), you need to inherit the RuntimeException class [throws is not required for this exception class ].

 

Class MyException extends Exception {public MyException () {} public MyException (String msg) {super (msg) ;}} public class Demo3 {static int div (int a, int B) throws MyException {if (B = 0) {throw new MyException ("an exception occurred! ");} Return a/B;} public static void main (String args []) {try {System. out. println (div (2, 0);} catch (Exception e) {System. out. println (e); // exception. myException: Zero division exception! }}}

 

 

Assertions:

 

  • Assertion is a common debugging method in software development.
  • Assertions are usually used to determine whether the program continues to execute when conditions are met. [For example, if you want to have dinner, you may not have enough assert to get it. Otherwise, you may eat air]
  • Enable assert: assert of eclipse and myeclipse is disabled by default. To enable assert, configure the Virtual Machine Parameters in perferences-> Java-> installed jres to-ea or-enableassertions.
  • In Java, assert is used to define assertions:Format: assert [boolean expression]

 

Public class Demo {public static void main (String [] args) {Boolean food = false; System. out. println ("prepare for dinner"); assert food; System. out. println ("rice comes ");}}

 

 

 

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.