One topic per day: Java exception handling

Source: Internet
Author: User
What is an exception

Ideally, the program will always run in a perfect environment, the network will not terminal, the file must exist, the program does not have a BUG. However, the ideal is very plump, the reality is very backbone, the actual production environment, the network may be interrupted, the file may not be found, memory may overflow, the program may have bugs. And these unexpected situations are anomalies.

In the unhandled case, the exception causes the program to not continue to execute, which affects the overall functionality of the software, but this is not allowed in most cases, so we need to handle the unhandled exception in the program, at least to ensure that the current task can safely exit.

Java exceptions

Java exception System, Throwable is a superclass, its subclasses include the error and Exception two classes, error mainly includes the JVM itself, this kind of problem is rare and cannot be solved by the program, so the code can not consider this part.

The Java exception system divides exceptions into check exceptions (checked exception) and non-checked exceptions (unchecked exception).

An unchecked exception can also be called a run-time exception, because it contains only runtimeexception in the Java exception system, mainly including the human-made exception when code is written, and these exceptions are not found in the code compilation phase, common Indexoutofboundsexception, NullPointerException, and indexoutofboundsexception, etc., can be determined to be a code BUG when an unchecked exception occurs.

Check exceptions include all exceptions except runtimeexception, common mainly IOException and sqlexcetion, this part of the exception may be artificial, but in the code compile phase can be found by the compiler, So you need to do the appropriate processing in the code.

Java Exception Handling

corresponding to the exception system, Java provides exception handling mechanism, commonly used keywords have try, catch, finally, throw and throws.

The use of try, catch, and finally is simple, just conform to the standard syntax, try to listen to the code block, when an exception occurs when the catch catch exception for exception handling, and finally execute a finally code block. There are several points to note:

    • Whether or not an exception occurs, the finally code block is executed, and the last execution
    • A catch can occur more than once, but there are several times when you need to be aware of the exception's parent-child relationship, and the subsequent exception cannot be the preceding subclass
try{    // 代码块} catch (异常){    // 异常处理代码} finally {    // 需要确保一定执行的代码,例如关闭流资源占用的代码}

Throw and throws are more easily confused. Throw is used to manually throw exceptions in code, while throws is used to declare exceptions that need to be thrown after a method signature, when a subclass method throws an exception when it involves an inheritance relationship, the parent method must throw an exception, cannot be a subclass of the subclass exception, and the number cannot be less than the subclass, and the code structure is as follows:

public void testMethod() throw Exception{    throw new RuntimeException();}
Suggestions for using Java exceptions
    • captures exceptions only when necessary or capable of processing, otherwise throws an exception to the caller until it is processed uniformly or logs the log when interacting with the page
    • finally block recommendations are only used to free up resources that are consumed, not to handle business logic
    • Do not handle runtimeexception through the exception handling mechanism, because such exceptions can be resolved by modifying the code, while increasing the robustness of the program
    • to record information that is useful for troubleshooting when the log is handled by an exception. The exception stack information and method run parameters are not determined when the
    • catch captures the exception as much granularity as possible, for example when capturing filenotfoundexception do not use IOException instead of
    • try Listen for as little code as possible, and do not place the entire method body in a try statement
Related Article

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.