Exception-throwable-error-exception-runtimeexcetpion-throw-throws-try catch in Java

Source: Internet
Author: User
Tags throwable try catch

When doing a function of converting a string to integer today, it is found that Integer.parseinte () throws an exception numberformatexception.

function integer.parseint (String) definition

1 int parseint (String s) 2                     Throws NumberFormatException

Test code:

 1  public  class   Test  2  { 3  public  static  void   main (string[] args)  4   { 5  Integer num = integer.parseint ("FF"  6   System.out.println (num);  7  }  8  }

We know that all the places that throw the exception require a try catch capture, or use throws to continue to throw up. So why is the code above compiled? Originally Java has exception and runtimeexception these two classes. RuntimeException is a subclass of Excetpion. If an exception is a subclass of RuntimeException rather than a subclass of exception, then we can capture it without the display of the try catch, but instead throw it to the JVM when an exception occurs. Of course we can also write the try catch to handle, not to the JVM. As in the following code:

1  Public classTest2 {3      Public Static voidMain (string[] args)4     {5         Try6         {7Integer num = integer.parseint ("FF");8 System.out.println (num);9}Catch(Exception e)Ten         { OneSYSTEM.OUT.PRINTLN ("The program has an exception!")); A         } -     } -}

There are a few common questions about exceptions in Java, which are listed below:

1) Exception class relationship

Throwable is the parent class for all exceptions. As common: NullPointerException and ClassNotFoundException

2) Error and exception

There are two subclasses under Throwable, one is exception and the other is error. So what's the difference between error and exception?

Error: Refers to JVM errors. This is what we will not encounter in our own writing program, which is the exception that the JVM throws itself during the run. This does not require too much attention from the programmer.

Exception: The parent class of exceptions that all users can handle. That is, the parent class of all the exceptions that we may encounter in the process of writing code is exception

3) Exception and RuntimeException

This is the subject of discussion at the beginning of this article. Exception is the parent class of RuntimeException. When the programmer writes the code, if it encounters a runtimeexception, you can choose whether to use try catch or throws to handle the exception, depending on the situation. If the exception encountered is the exception subclass, but not the RuntimeException subclass, then the try catch or throws that must be displayed in the program are processed.

4) The difference between throw and throws

Throw refers to the time when our program encounters an exception and is not processed in the current context. More narrowly, for example, if we are in a function, if there is an exception, we throw it directly with throws and leave it to the function call for processing, not in this function.

Throws refers to the fact that if a function throws an exception in a useful throw, you need to add a throws statement at the end of the function declaration, indicating that the current function throws an exception, notifying the function that the call was explicitly processed. Throws and throw are used in conjunction. As in the following code:

1  Public classTest2 {3      Public Static voidMain (string[] args)4     {5 dosomething ();6     }7     8      Public Static voidDoSomething ()throwsException9     { TenInteger num = integer.parseint ("FF"); One System.out.println (num); A     } -}

The above code cannot be compiled because an exception is thrown in dosomthing () and needs to be handled in the main function. The main function either adds a try catch statement, or throws the exception up to the JVM for processing. But generally do not do so. We always want the program to run correctly and exit. The following is the correct handling of the exception code:

1  Public classTest2 {3      Public Static voidMain (string[] args)4     {5         Try6         {7 dosomething ();8         }9         Catch(Exception e)Ten         { OneSystem.out.println ("The DoSomething function has an exception!" Exception information is as follows: "); A e.printstacktrace (); -         } -System.out.println ("main function exits normally!")); the     } -      -      Public Static voidDoSomething ()throwsException -     {  +Integer num = integer.parseint ("FF"); - System.out.println (num); +     } A}

Exception-throwable-error-exception-runtimeexcetpion-throw-throws-try catch in Java

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.