![]() The first call to the divideInt method is successful. Type in the command to run your program and hit Enter.Then type in the command to compile the source and hit Enter. Open a command prompt and navigate to the directory containing your Java program.Save your file as ThrowAnException.java.Of course, we could allow the arithmetic exception to be raised but the approach we're taking permits us to validate the divisor before attempting the ill-fated arithmetic. The second call passes a 0 for the divisor and will cause an exception to be thrown at runtime.Ě divisor of 0 would cause an arithmetic exception to be thrown by the JVM (Java Virtual Machine). The first call to the divideInt method will be successful. If an exception is thrown, then the message that was passed to the constructor of the exception will be displayed. The method calls are executed in a try catch block. Open your text editor and type in the following Java statements: The IllegalArgumentException is thrown at line 15 within the divideInt method.To learn how to throw an exception in Java, follow these four steps. This action will cause an exception to be raised and will require the calling method to catch the exception or throw the exception to the next level in the call stack. You can throw an exception in Java by using the throw keyword. How to Throw an Exception in Java See Java: Tips and Tricks for similar articles.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |