Signup as a Tutor

As a tutor you can connect with more than a million students and grow your network.

IT Courses/Java

What is the purpose of using throws keyword in java?

Asked by Last Modified  

Answer
Follow 8

Please enter your answer

10 Answers   4 Likes | 0 Dislikes

The throws statement is how you create an exception stack trace. There isn't much magic in java--it's not some underlying system mystery that creates an exception, it is simply a "Throw" statement, and therefore the last few entries in any stack trace will probably be throw. It's not JUST the last one because in many cases exception handling consists of something like this: try { do something that might cause an exception catch(ExpectedException e) { throw new DifferetnException(e); } This lets you change what type of exception it was or add a better textual description or even handle some of the exceptions while re-throwing the others. Because of this it is often the last few levels (and maybe some in-between) that are caused by the "Throw" statement.

Comments
Dislike Bookmark

Java J2ee Spring Angular JS Training

The 'throws' keyword is used to delegate the exception handling mechanism to the caller of the function. It is generally put in the function definition, thereby indicating to the caller of the function to handle it using try and catch. public static void fun() throws MyException { //code } public static void main (String args[]) { try { fun(); } catch { } }

Comments
Dislike Bookmark

Throws kW is used with method signature. With throws k/w exception is not handled. It should be handled with try catch block.With throws k/w checked exceptions can be propagated which can not be propagated with throw k/w.

Comments
Dislike Bookmark

Join Urbanpro.com, India?s most trusted education portal for your learning requirements

POST YOUR REQUIREMENT NOW

throwing an exception explicitly

Comments
Dislike Bookmark

If a function is not handling the exception throws clause can be added to the function signature. This includes the list of exceptions which can be thrown by the function

Comments
Dislike Bookmark

Salesforce Trainer

To Throw an exception

Comments
Dislike Bookmark

It is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained. Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.

Comments
Dislike Bookmark

Throws may be used, if we want o execute block of segment of code within it i.r throws section. if error encountered and we wish to execute certain code for that error we can use throws not necessary but may be use for better response.

Comments
Dislike Bookmark

Training On Java/Android/Web Development

Throws basically uses for declaring one or more exceptions. You can use it without declaring try-catch block

Comments
Dislike Bookmark

Java throws keyword is used to declare an exception. Checked exception can be propagated with throws. Throws is followed by class. Throws is used with the method signature. You can declare multiple exceptions e.g. public void method()throws IOException,SQLException.

Comments
Dislike Bookmark
Related Questions

IT Courses/Java

Why we are using this keyword in Java?

Asked by Kiran Bulkquestions 11/07/2017

Answer 7
Follow 5
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

Is synchronized keyword necessary, in Java, if everything that is modified in a function is local to that function?

Asked by Abishiek Bulkquestions 11/07/2017

Answer 5
Follow 1
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

What programming languages should a freshman learn if he already knows C, C++, Java and Python?

Asked by Sushmita Bulkquestions 11/07/2017

Answer 7
Follow 5
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

What is the use of the "this" keyword in Java?

Asked by Deepak Bulkquestions 11/07/2017

Answer 9
Follow 6
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

Which is the best programming language to learn in today's era? Java, C,C++, Python or any else?

Asked by Swati Bulkquestions 11/07/2017

Answer 5
Follow 3
Answer submitted successfully. ×

Please enter your answer

Looking for a Tutor?

Find best Tutors in your locality on UrbanPro.

FIND NOW

Now ask questions in any of the 1500+ Categories, and Get Answers from Tutors on UrbanPro.com

ASK A QUESTION

Related Lessons

Method Overloading vs Method Overriding

1. Method Overloading: Method overloading means a same method with same name can have different implementations/body by changing one of the follwing thing: 1) Number of Parameters 2) Order of Parameters 3) Type of Parameter i. Method overloading can be...

Difference Between Checked And Un-Checked Exception

1. Exception is an abnormal condition in the program, if raises program terminates abnormally. 2. Exception always occurs at runtime only, there is no chance of exception at compile time. 3. Syntactical mistakes or errors are occurs at compile time. 4....

Final, Finally And Finalize In Java

Final: 1. It is a non access modifier that can be applied to variables,methods and to class. 2. Final means non-changable. Final variable: 1. If a variable is declared as final it can't be re-assigned. 2. If there is any final variable in class simply...

Implementation: What Is JPA And JPA?

The basic Java framework to access the database is JDBC. Unfortunately, with JDBC, a lot of hand work is needed to convert a database query result into Java classes. JPA allows us to work with Java classes as it provides a transparent layer to each database...

Why We Should Go With Java?

Java is widely used in the Desktop application, web application, mobile development, games etc. Java is an object-oriented programming language. Its follow all the oops concept Like Object, Class, polymorphism, Inheritance, Encapsulation, Abstraction...

Sponsored

Find Best Java Training Classes?

Find Now »