IT Courses/Java

What is the purpose of using throws keyword in java?

Asked by Last Modified  

Answer
Follow 5

Please enter your answer

9 Answers   1 Likes | 0 DisLikes

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

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

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

POST YOUR REQUIREMENT NOW

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

throwing an exception explicitly

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

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

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
Related Questions

IT Courses/Java

Why we are using this keyword in Java?

Asked by Kiran Bulkquestions 11 Jul

Answer 6
Follow 4
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 Jul

Answer 6
Follow 3
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 Jul

Answer 8
Follow 5
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 Jul

Answer 5
Follow 2
Answer submitted successfully. ×

Please enter your answer

IT Courses/Java

Which programming language should one use for large scale machine learning, Java or C++?

Asked by Rayaan Bulkquestions 11 Jul

Answer 5
Follow 0
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

Session Tracking In Java Servlets

Session Tracking: HTTP is a stateless protocol. Each request is independent of the previous one. However, in some applications, it is necessary to save state information so that information can be collected from several interactions between a browser...

Core Java

Introduction: An Object Oriented Programming Language consists of Objects. There are object oriented programming languages like Smalltalk, C++. Each and every OOP language has some underlying disadvantages. C++ has some disadvantages like Security, Robust...

What Is The Exact Life Cycle Of A Servlet?

Java : Command Line Arguments

The parameters which are provided to the program at the command line. Eg:d:\>java a 10 20 30 Here,“java” is an interpreter, “a” is filename,10,20,30 are arguments passed to the program while executing the program And the notation...

Why Should We Learn Java Language?

Most of starters has confusion today that which of the programming language should be chosen to work on. It totally depends upon your need. As per my opinion, if you want to grow your career in IT industry it is good to choose Java Language because of...

Sponsored

Find Best Java Training Classes?

Find Now »