Java know how many () Java throws clauses

Source: Internet
Author: User
Tags java throws

If a method can cause an exception but does not handle it, it must specify this behavior so that the callers of the method can protect themselves without exception. Do that. You can include a throws clause in a method declaration. A throws clause lists all the exception types that a method might throw. This is necessary for all exceptions of type except error or runtimeexception and their subclasses. All other types of exceptions that a method can throw must be declared in the throws clause. Failure to do so will result in a compilation error.

The following is a common form of a method declaration that contains a throws clause:

1 throws exception-list{2     //  Body of method3 }

Here, Exception-list is the list of exceptions that the method can throw with a comma-delimited exception.


The following is an incorrect example. This example attempts to throw an exception that it cannot catch. Because the program does not specify a throws clause to declare the fact, the program will not compile.

1 //This program contains an error and would not compile.2 classThrowsdemo {3     Static voidThrowone () {4System.out.println ("Inside throwone.");5         Throw NewIllegalaccessexception ("Demo");6     }7      Public Static voidMain (String args[]) {8 Throwone ();9     }Ten}

To compile the program, you need to change two places. First, declare Throwone () to throw a Illegalaccess exception exception. Second, main () must define a Try/catch statement to catch the exception. The correct examples are as follows:

1 //This was now correct.2 classThrowsdemo {3     Static voidThrowone ()throwsillegalaccessexception {4System.out.println ("Inside throwone.");5       Throw NewIllegalaccessexception ("Demo");6    }7     Public Static voidMain (String args[]) {8       Try {9 Throwone ();Ten}Catch(illegalaccessexception e) { OneSystem.out.println ("Caught" +e); A       } -    } -}

The following is an example of the output:

1 inside Throwone 2 caught Java.lang.IllegalAccessException:demo
Series Articles:

Java know how much (top)

Java know how much (interface) interface

Java knows how much (40) the difference between an interface and an abstract class

Java know how much (41) generic explanation

Java know how much (42) the range of generic wildcard characters and type parameters

Java know how much (43) Exception Handling Basics

Java know how much (44) exception type

Java know how much (45) uncaught exceptions

How much Java knows (the) use of try and catch

Java know how much (47) use of multiple catch statements

Java knows how much (in) the nesting of Try statements

Java know how much (a) throw: Exception throws

Java know how many () Java throws clauses

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.