java throws keyword /Chapter 10 / Exception Handling

java throws

Java throws keyword


We can use throws keyword to delegate the responsibility of exception handling to the caller. It may be another method or JVM. After then caller method is responsible to handle that exception.

example:-
class Test
{
public static void main(String [] args) throws InterruptedException
{
Thread.sleep(5000);
}
}




Conclusions:-

  1. throws keyword required only for checked exceptions.If we are using for unchecked exceptions then there is no use of throws keyword.

  2. throws keyword used for  convincing the compiler that we have handling the exception, it doesn't prevent abnormal termination of program.

  3. We can use throws keyword for methods and constructor but not for classes.
    class Test throws Exception //invalid here
    {
    Test () throws Exception // valid here
    {
    }
    public void m1() throws Exception //valid here
    {
    }
    }

     

  4. We can use throws keyword only for Throwable types. If we are using for any other type then we will get compile time error.


 

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com