Java High Salary Road __002_ exception handling

Source: Internet
Author: User

Custom exceptions
1  Packagelearn. Javabasics.class;2 3 Importjava.util.Date;4 5 /**6 * How to customize an exception class7 * 1. Inherit an existing exception class8 * 2. Provide a serial number that provides several overloaded constructors9 * 3. Use as if it were an exceptionTen  */ One  Public classMyExceptionextendsRuntimeException { A     Private Static Final LongSerialversionuid = -1201746919629597153l; -  -      Publicmyexception () { the          -     } -      -      Publicmyexception (String msg) { +         Super(msg); -     } +      A      Public Static voidMain (String ... args) { at         Try { -             Throw NewMyException ("Error in my exception" +NewDate ()); -}Catch(Exception e) { -             //TODO auto-generated Catch block - e.printstacktrace (); -         } in     } -}
View Code

In subclasses overriding the method of throwing exceptions to the parent class, the exception thrown in the subclass cannot be greater than the exception of the parent class, up to equal

Java.lang.Throwalbe |-----error : Errors, not processed in the program |----- Exception: Exception, which requires that the handling of these exceptions be considered when writing the program |---- Compile-time exceptions, exceptions that occur during compilation (an exception occurs when the Javac.exe command is executed) |----Run-time exception, an exception that occurs during run time (an exception occurs when the Java.exe command is executed)

Java High Salary Road __002_ exception handling

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.