Java exception-run-time exception

Source: Internet
Author: User

There is a special subclass exception RuntimeException run-time exception in/*exception. If the exception is thrown in the function's contents, the function can be passed without declaration and compilation. If the exception is declared on the function. The use of the whine can not be processed. Compiled as passed. It is not necessary to declare the function because it does not need to be handled by the caller. When the exception occurs, you want the program to stop. Because at run time, there is no way to continue the operation, you want to stop the program, the code is modified. When you customize an exception, you cannot continue the operation if the exception occurs, and you let the custom exception inherit runtimeexception. A run-time exception that causes the program to stop. So when customizing exceptions, either inherit the RuntimeException or inherit the exception. Summary: There are two types of exceptions: 1, the exception that is detected at compile time: (for the exception that can be handled) is a non-runtime exception, and the exception is detected at compile time. This is not only to declare on the function (tell the caller that this function will have an exception, let the caller throw or resolve the exception), but also to make a corresponding processing action inside the function that called the function, either throw it on the function or Try...catch catch the exception. 2, the exception that is not detected at compile time: (for exceptions that cannot be handled, you can only stop the program and modify the code to resolve the exception) (Run-time exception.) RuntimeException and its subclasses. ) */class Fushuexception extends Runtimeexception{fushuexception (String msg) {super (MSG);}} Class Demo{int div (int a,int b)//throws ArithmeticException//When a function calls the div () function, either continues to throw an exception upward, which is thrown to the virtual machine. Either catch the exception and handle the exception. {//The exception that inherits the RuntimeException runtime exception is not declared on the function, but can also be compiled, which is a feature of runtime exceptions. if (b==0) throw New ArithmeticException (); return a/b;}} Class ExceptionDemo2 {public static void main (string[] args)//throws arithmeticexception--This practice is to throw the exception thrown by the function and throw it to the virtual machine for processing。 {Demo D=new demo ();/*try{int x=d.div (4,0); System.out.println ("x=" +x); catch (ArithmeticException E)//catches the exception thrown by the function and handles the exception. {System.out.println (e.tostring ());} */int X=d.div (4,0); System.out.println ("x=" +x); System.out.println ("Hello world!");}}

Java exception-run-time exception

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.