Flow Control in Java. Loops in Java. Jump Statements in Java. Arrays in Java. Strings in Java. OOPS in Java. Constructors in Java. Interfaces in Java. Keywords in Java. Exception Handling in Java. Collection Framework. Multi-threading in Java. Table of Contents. Save Article. Improve Article. Like Article. Previous Flow control in try catch finally in Java. The throw keyword can be useful for throwing exceptions based on certain conditions within a code block and for throwing custom exceptions.
The throws keyword in Java is used to declare exceptions that can occur during the execution of a program. For any method that can throw exceptions, it is mandatory to use the throws keyword to list the exceptions that can be thrown. The throws keyword provides information about the exceptions to the programmer as well as to the caller of the method that throws the exceptions. The throws keyword allows exceptions to be propagated in the call stack.
When a method declares that it throws an exception, it is not required to handle the exception. The caller of a method that throws exceptions is required to handle the exceptions or throw them to its caller and so on so that the flow of the program can be maintained. Only checked exceptions are required to be thrown using the throws keyword. Here is an example of a method that throws an exception, which is handled by the caller of the method:. As seen in the syntax above, all exceptions that can be thrown by a method should be declared in the method signature using the throws keyword.
A method can throw multiple exceptions, which should be separated by a comma in the declaration. The throw keyword in Java is used for explicitly throwing a single exception. This can be from within a method or any block of code. Both checked and unchecked exceptions can be thrown using the throw keyword. The method removes the top element from the stack and returns the object. The pop method checks to see whether any elements are on the stack. If the stack is empty its size is equal to 0 , pop instantiates a new EmptyStackException object a member of java.
The Creating Exception Classes section in this chapter explains how to create your own exception classes. For now, all you need to remember is that you can throw only objects that inherit from the java. Throwable class. Note that the declaration of the pop method does not contain a throws clause. EmptyStackException is not a checked exception, so pop is not required to state that it might occur.
The objects that inherit from the Throwable class include direct descendants objects that inherit directly from the Throwable class and indirect descendants objects that inherit from children or grandchildren of the Throwable class. The figure below illustrates the class hierarchy of the Throwable class and its most significant subclasses.
As you can see, Throwable has two direct descendants: Error and Exception. When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an Error.
Simple programs typically do not catch or throw Error s.
0コメント