Java | Exception handling | Throws-Throw keyword :

           ~~ CORE-JAVA ~~

 1. INTORDUCTION

     1.1 Java Intorduction
     1.2 Java history
     1.3 Java features
     1.4 Diff b/w Java & C++
     1.5 Java Variables
     1.6 Java Data Types
     1.7 Typecasting
     1.8 Arrays

 2. OOPS

     2.1 Introduction
     2.2 Class
     2.3 Object
     2.4 Static Keywords
     2.5 Constructors
     2.6 This KeyWord

 3. INHERITANCE

     3.1 Inheritance (IS-A) 
     3.2 Aggregation (HAS-A)

 4. POLYMORPHISM

     4.1 Polymorphism & Its Type
     4.2 Method Overloading
     4.3 Method Overriding
     4.4 Super Keyword
     4.5 Final Keyword
     4.6 Dynamic Binding

 5. ABSTRACTION

     5.1 Abstract Classes & Methods
     5.2 Interfaces
     5.3 Abstract vs Interface

 6. ENCAPSULATION

     6.1 Encapsulation

 7. STRING MANIPULATIONS

     7.1 String
     7.2 String Buffer
     7.3 String Tokenizer

 8. PACKAGES

     8.1 Predefined packages
     8.2 Userdefined Packages
     8.3 Access Specifiers

 9. EXCEPTION HANDLING

     9.1 Introduction
     9.2 Try-Catch-Finally
     9.3 Throws-Throw Keyword
     9.4 Built-in Exception
     9.5 Customized Exception

 10. MULTITHREADING

     10.1 Introduction
     10.2 Thread Creations
     10.3 Thread Life Cycle
     10.4 Life Cycle Methods
     10.5 Synchronization
     10.6 Wait() notify() notify all()

 11. WRAPPERCLASSES

     11.1 Introduction
     11.2 Wrapper Classes

 12. COLLECTION FRAME WORK

     12.1 Introduction
     12.2 List interface
     12.3 Set interface                     12.4 Map interface                           

 13. INNER CLASSES

     13.1 Introduction
     13.2 Member inner class
     13.3 Static inner class
     13.4 Local inner class
     13.5 Anonymous inner class

 14. CoreJava Interview Questions

Throws Keyword :

  • In our program, If there is any chance of Checked Exception then compulsory we need to handle it. Otherwise we will get CompileTime Error. We can understand this thing in below simple Example.

Example :

 

  • This type of Exception, we can handle by using below 2 ways :
    1. By Try-Catch Block.
    2. By Throws Keyword.

 

1. By Try-Catch Block :

//need to write our program

 

2. By Throws Keyword :

//need to write our program

 

NOTE : 

  • Here we can say, the main purpose of Throws keyword is to delegate the responsibility of Exception Handling in the case of Checked Exception.
  • In Case of Unchecked Exception, It is not required to use Throws Keyword. 

 

Throw Keyword :

  • There is Throw Keyword also available in Java, which helps us to throw an exception explicitly and catch it.
  • In general we can use Throw Keyword for Customized Exceptions.
  • After Throw Keyword we are not allow to write any statement directly otherwise we will get CompileTime Error.
  • Below is an example, how to use Throw Keyword in our program :

 

Example :

Help Others, Please Share