In Java, a programmer must throw a possible exception when the defined method may produce an exception.
Two ways of throwing exceptions, one is to use a throw statement inside a method, and the other is to use the throws keyword in the method definition
1.throw statements
In Java, the throw statement is used to throw an exception. The specific type of the exception can be defined by the programmer itself, or it can be a Java system exception type.
Remember, an exception is an object of an exception class, and when an instance of an exception class is available, you can use the throw statement to throw a
Syntax rules:
Throw Instanceexception Exception Object
2.throws keywords
To indicate that some code in your defined method may produce an exception, use the keyword throws when defining the method with the name of the exception thrown by the method.
Syntax rules:
..... Method Name () throws Exception class
In many cases we only define a method that throws an exception, but not to catch or handle that exception, which can be thrown using throws. A more reasonable usage is to handle exceptions when calling this method.
For example:
public static dosomething () throws runtimeexcep{
...........
}
Java exception throws