What does 'throw' do in exception handling, Java?
-
I am trying to understand the throw clause in Java while I am reading from a book, but I didn't understand why throw clause is declared inside try scope. Quoted from ...show more
-
Answer:
Throw is something that will allow you to provide a more generic explanation of your to end user... If we consider the same example you gave, suppose in your try catch block, division is taking place and if number is divided by 0 than an exception will be thrown and shown to user as below : "java.lang.AritmeticException : Division by 0". This message is more technical to end user. Now if you want a more generic message to be shown to user, than we can customize the message and throw it as exception, so if you encounter a situation in your try block and you are sure that it will raise an exception, so instead of default value you can give appropriate message and throw that exception. For e.g "throw new java.lang.ArtihmeticException("Number cannot be divided by 0") ".
FCSDIF4SYPGYEPAXBKWSVNKOYE at Yahoo! Answers Visit the source
Other answers
You use throw to tell the program that in the following code/method there might happen an exception. When you dont do this the program will just stop running. But this way you can also tell the user why the program stopped. It is used to make the program more user friendly. And by telling ArithmeticException you also tell the computer what kind of errors might happen. It would work just fine even with the Throw now Exceptionement statement
Joana
it allows you to play games
Boss
Related Q & A:
- What are the best resources to learn Java?Best solution by Stack Overflow
- How to handle exception in Java?Best solution by javatpoint.com
- What ski movie is throw it up in?Best solution by Yahoo! Answers
- What did people used to throw at miscreants in the stocks and did they ever throw anything smelly?Best solution by Yahoo! Answers
- My FedEx tracking update says delivery exception what does it mean?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.