Other concepts of exceptions-throw, throws, Exception, RuntimeException, and assertions

Source: Internet
Author: User

Assume that a division method div () is defined. For Division, exceptions may occur and exceptions may not occur. For this method, it is best to use the throws keyword for declaration. Once an exception occurs, it is handed over to the call center for processing. Because the div () method uses the throws keyword declaration. Therefore, you must handle exceptions when calling this method. [Java] class Math {public int div (int I, int j) throws Exception {// defines a division operation. If an Exception exists, int temp = I/j; // computation will be handled at the called place, but return temp may be abnormal here ;}}; public class ThrowsDemo01 {public static void main (String args []) {Math m = new Math (); // instantiate the Math class object try {System. out. println ("division operation:" + m. div (10, 2);} catch (Exception e) {e. printStackTrace (); // print exception }}; if the throws keyword is used in the declaration of the main method, the main method can also not handle the exception. All exceptions are handed over to the JVM for processing. [Java] class Math {public int div (int I, int j) throws Exception {// defines a division operation. If an Exception exists, int temp = I/j; // computation will be handled at the called place, but return temp may be abnormal here ;}}; public class ThrowsDemo02 {// All exceptions in the main method do not use try... catch to process public static void main (String args []) throws Exception {Math m = new Math (); // instantiate the Math class Object System. out. println ("division operation:" + m. div () ;}}; 2. In exception handling, the try statement captures an exception object. You can also throw this exception object by yourself. Output. [Java] public class ThrowDemo01 {public static void main (String args []) {try {throw new Exception ("throwing it on your own. "); // Specifies the instantiated object that throws an Exception} catch (Exception e) {System. out. println (e) ;}}; 3. Example of throw and throws application: To design a division method, however, before performing an operation, you must print the "computing start" Information and "computing end" information. If an exception exists, the exception should be handled by the called place. [Java] class Math {public int div (int I, int j) throws Exception {// defines the division operation. If an Exception exists, it is handed over to the called place for processing System. out. println ("****** computing start ******"); int temp = 0; // define the local variable try {temp = I/j; // calculate, however, an Exception may occur here.} catch (Exception e) {throw e;} finally {// whether an Exception exists or not, you must execute the uniform exit System. out. println ("****** computing end ******");} return temp ;}}; public class ThrowDemo02 {public static void main (String args []) {Math m = new Math (); Try {System. out. println ("division operation:" + m. div (10, 0);} catch (Exception e) {System. out. println ("Exception generation:" + e) ;}}; 4. Differences between Exception and RuntimeException [java] public class RuntimeExceptionDemo01 {public static void main (String args []) {String str = "123"; // defines a String consisting of digits: int temp = Integer. parseInt (str); // convert string to int type System. out. println (temp * temp); // calculate the multiplication party}; parseInt () method Definition Format: public static I Nt parseInt (String s) throws NumberFormatException this method clearly uses the throws keyword to throw an exception. why can it be compiled without processing ?? 5. custom Exception classes can be customized only by inheriting exceptions. [Java] class MyException extends Exception {// custom Exception class, inherits the Exception class public MyException (String msg) {super (msg ); // call the constructor of a parameter in the Exception class to pass the error message}; public class DefaultException {public static void main (String args []) {try {throw new MyException ("custom exception. "); // Throw an Exception} catch (Exception e) {System. out. println (e );}}}

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.