简体   繁体   中英

public static void main(String[] args) throws Exception

What is the meaning of this?

public static void main(String[] args) throws Exception?

I don't know it. If someone know please help me. I really want to know about "throws Exception".

  • public : it is a access specifier that means it can be accessed by any other class in the program.

  • static : it is access modifier that means when the java program is load then it will create the space in memory automatically.

  • void(return type) : it does not return any value.

  • main() : it is a method or a function name.(First method to execute by JVM)

  • string args[] : its a command line argument it is a collection of variables in the string format.

  • throws Exception : Use exceptions to notify about things that should not be ignored.

It's three completely different things:

public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details.

static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.

void means that the method has no return value. If the method returned an int you would write int instead of void.

It is simply the usual entry point method public static void main(String[]) , except that it explicitly specifies an exception may be thrown. This is required by the compiler if any part of your code explicitly throws an exception without a try-catch block (excluding of course runtime-exceptions). For example, this will not compile:

public static void main(String[] args){
    throw new Exception();
}

Those three keywords are pretty much different from one another. Public=This type can be called by any place from the program. not protected from other classes. Static=This type of methods does not have to be instantiated in order to invoke them. Void=This type of methods does not have a return value

The reason this main method is throwing an Exception outside of main is because the rest of the actual program's implementation doesn't actually care at all about what that Exception is about.

For example, if all I was doing in my program was printing something out onto the screen repeatedly every 5 seconds, I wouldn't really care too much about the InteruptedException being thrown due to the Thread.sleep() method. In such cases, we would then just throw it outside of main to just discard of its irrelevance instead of having to write some kind of code to handle it.

There's honestly nothing special about it at all. It's just a way to not have to fool with something meaningless.

public class IrrelevantExceptionExample {
    /**
     * Main method that repeatedly prints to the console every five seconds from a single Thread.
     * @param args - Wise words from above.
     * @throws InterruptedException - Uhm… There's only one Thread and it's sleeping! (IrrelevantException)
     */
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Hey, at start up.");
        for (int i = 5; i < 25; i += 5) {
            Thread.sleep(5000);
            System.out.printf("Hey, again, except %d seconds later!%n", i);
        }
    }
}

Public: it is an access specifier which can be access throughout the program. Static: it is a keyword which can be used for single copy of that variable. Void: it is an empty return type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM