![]() Note: If we throw unchecked exception from a method, it is must to handle the exception or declare in throws clause. Similarly, we can also throw unchecked and user defined exceptions. The above code throw an unchecked exception. ![]() Throw new ArithmeticException("Person is not eligible to vote") throw Arithmetic exception if not eligible to vote function to check if person is eligible to vote or not The syntax of the Java throw keyword is given below. ![]() Here, we just need to set the condition and throw exception using throw keyword. For example, we can throw ArithmeticException if we divide a number by another number. We can also define our own set of conditions and throw an exception explicitly using throw keyword. We will discuss custom exceptions later in this section. It is mainly used to throw a custom exception. We can throw either checked or unchecked exceptions in Java by throw keyword. These exceptions may be related to user inputs, server, etc. The Exception has some message with it that provides the error description. We specify the exception object which is to be thrown. The Java throw keyword is used to throw an exception explicitly. In Java, exceptions allows us to write good quality codes where the errors are checked at the compile time instead of runtime and we can create custom exceptions making the code recovery and debugging easier.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |