7.4 Custom Exception classes
Defining an exception class only requires inheriting the exception class.
Example: Custom Exception class
Class:myexception
PackageLimethrowable._7_4; Public classMyExceptionextendsException {//Custom exception classes, inheriting the exception class /** * */ Private Static Final LongSerialversionuid = 1L; //Construction method receives exception information Publicmyexception () {Super(); } PublicMyException (String message, throwable cause,BooleanEnablesuppression,Booleanwritablestacktrace) { Super(message, cause, enablesuppression, writablestacktrace); } Publicmyexception (String message, throwable cause) {Super(message, cause); } Publicmyexception (String message) {Super(message); } Publicmyexception (Throwable cause) {Super(cause); }}
Class:main
Package Limethrowable._7_4; Public class defaultexception { publicstaticvoid main (string[] args) { Try { thrownew myexception ("Custom exception. "); } Catch (Exception e) { System.out.println (e); }}}
Console:
Limethrowable._7_4.myexception: Custom Exception.
7.5 Assertions
La La la
Java--Exception capture and processing--Custom exception classes