final vs finally vs finalize

Difference between final, finally & finalize keywords in java is :

S.Nofinalfinallyfinalize
1Final is used to apply restrictions on class, method and variable. Final class can’t be inherited, final method can’t be overridden and final variable value can’t be changed.Finally is used to place important code, it will be executed whether exception is handled or not.Finalize is used to perform clean up processing just before object is garbage collected.
2Final is a keyword.Finally is a block.Finalize is a method.

Examples :

1. Final keyword :

      
class FinalKeywordExample {
  public static void main(String[] args) {
    final int x = 10;
    x = 20;// Compile Time Error
  }
}
  

2. Finally block :

    
class FinallyKeywordExample {
  public static void main(String[] args) {
    try {
      int x = 30;
    } catch (Exception e) {
      System.out.println(e);
    } finally {
      System.out.println("finally block is executed"); // this gets printed
    }
  }
}

3. Finalize method :

    
class FinalizeKeywordExample {
  public void finalize() {
    System.out.println("finalize called");
  }

  public static void main(String[] args) {
    FinalizeKeywordExample f1 = new FinalizeKeywordExample();
    FinalizeKeywordExample f2 = new FinalizeKeywordExample();
    f1 = null;
    f2 = null;
    System.gc();
  }
}

Summary

So here we are, we hope now you got to know difference between final, finally and finalize keyword in java.

We tried our best to simplify the explanation, if you feel any difference or you need help in understanding any such thing, then feel free to contact us through email and subscribe to ProgrammerToday.


Throw vs Throws in Java

Difference between throw and throws keyword in java exception handling

S.Nothrowthrows
1Throw keyword is used to create a new Exception object and throw itThrows keyword is used in method definition, to declare that a method can throw this kind of exception.
2Using throw keyword you can declare only one Exception at a time.Using throws keyword you can declare multiple exception at a time.

Summary

So here we are, we hope now you got to know difference between throw and throws keyword in Exceptions in java.

We tried our best to simplify the explanation, if you feel any difference or you need help in understanding any such thing, then feel free to contact us through email and subscribe to ProgrammerToday.


Throws keyword in java with Example

Throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself. It is used to declare an exception, it tells the programmer that an exception may occur hence warning the programmer to also write exception handling mechanism to catch the exception.

It is usually used to throw checked exceptions and not unchecked ones. As unchecked exceptions can be controlled and exception handling mechanism can be used to catch it, so now the checked exceptions can be propagated/forwarded to another calling method.

You can throw multiple exceptions from a single method by seperating them using comma.

Example : Check exception propagation using throws keyword

    
import java.io.IOException;

public class ThrowsException {

  static void testMethod(int num) throws IOException, ClassNotFoundException {
    if (num == 1)
      throw new IOException("** IOException Occurred here **");
    else
      throw new ClassNotFoundException("** ClassNotFoundException **");
  }

  public static void main(String[] args) {

    try {
      testMethod(1);
    } catch (Exception ex) {
      System.out.println(ex);
    }

  }

}

OUTPUT

** IOException Occurred here **

Important : Why do we need throws ?

Why do we need throws when we can handle exceptions using try catch block ?

This is a valid question right ?

We need it for 2 important reasons :

  • In a scenarios where you have to handle multiple exceptions in various methods, it becomes very tedious to write try catch block in all the methods, so here comes throws into picture, we use throws in the method definition of various methods but catch them in one of the calling method.
  • Another advantage is that you will be forced to handle all the exceptions which is a good practice otherwise you will get compilation errors.

Summary

So here we are, we hope now you are confident and know how to use throws keyword in Exceptions in java.

We tried our best to simplify the explanation, if you feel any difference or you need help in understanding any such thing, then feel free to contact us through email and subscribe to ProgrammerToday.


Throw keyword in java with Example

It is used to create a new Exception object and throw it. Using throw keyword you can declare only one Exception at a time.

It is mainly used to throw custom exceptions.

One can also say Throw keyword is used to transfer control from try block to catch block.

Syntax :

throw new IOException("cannot open new connection");  

Example : To show throw Exception usage

    
public class TestThrowInException {

  static void validate(Integer salary) {
    
    int gross;
    if (salary == null)
      throw new NullPointerException("not valid");
    else {
      gross = salary + 1000;
    }
    System.out.println("gross salary is :" + gross);
  }

  public static void main(String[] args) {

    try {
      validate(null);
    } catch (Exception e) {
      System.out.println(e);
    }
  }

}

OUTPUT

java.lang.NullPointerException: not valid

Summary

So here we are, we hope now you are confident and know how to use throw keyword in Exceptions in java.

We tried our best to simplify the explanation, if you feel any difference or you need help in understanding any such thing, then feel free to contact us through email and subscribe to ProgrammerToday.