Java checked exceptions and runtimeexception (run-time exceptions) __java

Source: Internet
Author: User
Tags throw exception

Directory

   First, the Run-time anomaly

     1, what is Runtimeexceptioin
     2, the characteristics of the Run-time anomaly
     3, how to use the Run-time anomaly

   two, run-time anomaly and ckecked difference

     1, mechanism
     2, logically  

One, Run-time exception

1, what is the run-time exception

The exception that occurs during the run of the program, Rumtimeexception is a subclass of Exception
We can view the Java API documentation

Stick a sketch Map

2, the characteristics of abnormal operation

In general, we have an exception in the method body, we use the Throw keyword to throw the exception object or the exception object reference, if the current method can not handle the exception, then the method must be in the argument list after the method body must use throws declaration exception belongs to the class, to the caller to handle. But RuntimeException is a very special subclass, you can not throw and throws, even if you throw, there is no need to thtows, even if you throws, the caller is not necessary try-catch

1> if the class exception or its subclass exception is thrown in the content of the function, the function can be passed without declaring, as compiled

2> If the exception is declared on a function, the caller can handle it differently (Try-catch) and compile the same

Why not use a statement.

The reason why you do not have to declare on a function is that it does not need to be handled by the caller, and when the exception occurs, you want the program to stop because, at run time, there is a situation where the operation cannot continue, and the programmer will revise the code when the program stops.

3. How to use the Run-time anomaly

Custom Exception class inherits RuntimeException when the exception occurs and cannot continue running.

And then stick to the wait (long Timeout) method in the simple schematic Object, and continue to view the Java API Document's description of the method

The difference between run-time anomaly and ckecked anomaly

There are two types of exceptions: Check for exceptions and unchecked exceptions (Run-time exceptions)

Check Exception : Compile-time detected exception (throw, the method has the ability to deal with try-catch processing, no ability to deal with must throws). Compilation does not pass, check the syntax (in fact, is throw and throws supporting use).

run-time Exceptions : Exceptions that are not checked at compile time (run-time exceptions). RuntimeException and its subclasses). Compiled through.

1, the Mechanism

The main performance in the following two aspects:
(1) How to define the method
(2) How to handle thrown exceptions

Runtime exceptions, you do not need to throw an exception object with a throws declaration, or you can throw an exception object or an exception reference without throw. For this method to be invoked, there is no need to place it in the Try-catch code block. Why If you capture it, there is a risk that the program code error will be obscured and undetectable in operation.

And check for exceptions: Once you throw an exception with throw, if the current method can handle the exception, then use Try-catch to handle it directly within the method. If the current method does not have the ability to handle the exception, then the class that the exception belongs to must be declared with throws in front of the method body after the argument list, to be handled by the caller (method) that called the method.

2, logically

From a logical point of view, checked exceptions and runtimeexception are used for different purposes, and check exceptions are used to indicate an exception that can be handled directly by a caller (for example, user input errors, programs can be directly captured and processed, prompting user input errors), RuntimeException is used to refer to a program error that the caller itself cannot process or reply to (for example, you encapsulate a library for others, and when someone calls a method in your library, you need to pass in some parameters, and if the user's incoming arguments are illegal, you can't handle it. Then the assassin you throw should be a run-time exception.

I personally feel that runtimeexception is a bit like the assertion in OC (Nsalert) .

further thinking : The previous knowledge point has dropped to, carry the parameter the construction method can assign the initial value to the object, but also can assign a reasonable value to the object. For example, initializing a rectangular object, using the construction method given width and length, we can judge the rationality of the long width value in the construction method, but if you initialize the object at this time, you call the method of the area, return a value, then this value is the exception value. According to the knowledge, we will generally be in the area of the method, judge the rationality of the value, if unreasonable throw out an exception. But, conversely, if I were to throw an exception to the width and length you passed in at first, it would be a good thing to simply initialize the object without success. Because you may not know how to achieve my method of area, even if I throw you an exception, you do not know how to deal with. (Through this, we also understand that a small knowledge point, the construction method can also throw an exception, and, the type of throw exception is usually a Run-time exception)

Related Article

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.