-1-6 Java Exceptions Simple Introduction Java exception system Throwable classification throws and throw exception handling custom exceptions

Source: Internet
Author: User
Tags throw exception throwable

ExceptionException: An exception is an error that occurs while the Java program is running. Exception Origin: The problem is also a real life of a specific transaction, can also be described in the form of Java classes, and encapsulated into objects. In fact, Java is a description of the abnormal situation after the object. Throwable|--error Serious problems that we do not deal with. |--exception|--runtimeexception Run-time exception, we need to fix the code|--Non-runtimeexception compile-time exception, must be handled, or program compilation does not pass  the subclass names for exception and error are suffixed with the name of the parent class. Exceptions in Java are divided into two main classes: compile-time exceptions and run-time exceptions. all instances of the RuntimeException class and its subclasses are called run-time exceptions, and other exceptions are compile-time exceptionscompile-time exceptions? The Java program must show processing, or the program will have an error and cannot be compiledrun-time exception? Do not need to display the processing, can also be treated as a compile-time exception Throwable Basic Methods
getMessage () ? Gets the exception information, which returns a string. toString () ? Gets the exception class name and exception information, and returns a string. printstacktrace () ? Gets the exception class name and exception information, and the location where the exception appears in the program. return value void. Printstacktrace (printstream s) ? This method is typically used to store exception content in a log file for review.  
throws and throwthrowsused after the method declaration, followed by the exception class name? can be separated by a comma with multiple exception class namesrepresents a thrown exception, handled by the caller of the method? throws represents a possibility of an exception that does not necessarily occurThrowused in the body of a method, followed by the name of the exception object? Only one exception object name can be thrown? represents throwing an exception, handled by a statement in the method bodyThe throw throws an exception, and the throw throws a certain kind of exception .
  Exception Handling Procedures  default handling of the JVMThe exception name, reason, location and other information are output in the console, but the program cannot continue to execute. Handle it yourselftry...catch...finallyWrite your own processing code, the following program can continue to executethrowsyou can't handle it, declare it on the method, tell the caller, there's a problem .

Try

{

code that needs to be detected;

}

Catch

( Exception class variable )

{

exception handling code;

}

Finally

{

code that is bound to execute ;

}

Finally code block -controlled statement bodies are bound to execute

only one situation will not be enforced. It was before the system.exit (0)was executed.

Primarily used to release resources

common forms of deformationtry...catch...finallyTry...catch ...Try...catch...catch ...try...catch...catch...fianllytry...finally Custom Exceptionsa custom class inherits exception or its subclasses, and only needs to provide a parameterless construct and a parameter construct.The exception information is defined by a constructor function.

Cases:

extends exception{  demoexception (String message)  {  Super(message);}  }

throws a custom exception by throw. RuntimeException and its subclasses can be thrown out of a function without being declared on the function. 1. The parent's method throws an exception, and the child's overriding method must be less than or equal to the parent's exception when throwing the exception, that is, the subclass that must throw the same exception or exception2. The parent's method does not throw an exception, the child's overriding method cannot have an exception thrown, if there is an exception in the subclass method, then the subclass can only try, not throws3. The parent's method throws multiple exceptions, and the child's overriding method must be less or smaller than the parent and cannot throw a new exception

-1-6 Java Exceptions Simple Introduction Java exception system Throwable classification throws and throw exception handling custom exceptions

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.