How do I throw an exception when I receive a signal?

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

Was this solution helpful to you?

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

Just Added Q & A:

Find solution

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.