Java Tutorial: The Throws/Throw Keywords | W3Schools

The Throws/Throw Keywords

If a method unable to manage a checked exception then the method must declare if it is using throw keyword. This keyword will appear at the end of method’s signature. You can throw the exception, either in a newly instantiated way or an exception you caught by using that throw keyword.
Attempt to comprehend the distinction amongst throw and throws keywords,throws is utilized to put off the treatment of a checked special case and toss is utilized to summon an exemption expressly.
The accompanying technique announces that it tosses a RemoteException −
Example
import java.io.*;
public class className {

public void deposit(double amount) throws RemoteException {
// Method implementation
throw new RemoteException();
}
// Remainder of class definition
}

This method proved that it throws multiple exception, and in which case exceptions are declared in a separated list with Commas. For illustration, this method proves that it throws a remoteexception as well as an Insufficient Funds Exception.

Example

import java.io.*;
public class className {
public void withdraw(double amount) throws RemoteException,
InsufficientFundsException {
// Method implementation
}
// Remainder of class definition
}

The Finally Block
This method follows a catch block and a try block. This method code always executes, regardless of occurrence of an Exception.

This block allows user to run away all cleartype statements which you want to execute, in spite what happens in that protected code.

final catch block have the follwing syntax:

Syntax

try {
// Protected code
}catch(ExceptionType1 e1) {
// Catch block
}catch(ExceptionType2 e2) {
// Catch block
}catch(ExceptionType3 e3) {
// Catch block
}finally {
// The finally block always executes.
}

Example

public class ExcepTest {
public static void main(String args[]) {
int a[] = new int[2];
try {
System.out.println(“Access element three :” + a[3]);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println(“Exception thrown :” + e);
}finally {
a[0] = 6;
System.out.println(“First element value: “ + a[0]);
System.out.println(“The finally statement is executed”);
}
}
}

the following result will be following:

Output
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed
Note this:
1. Existence of catch cluase is not possible wihout a try statement
2. Try block can not be found without either funally clause or cathc clause.
3. No code can be present in these blocks finally, catch and try blocks.
The try-with-resources:
Usually, when we make use of any resources as connections, streams, etc. We have to close them explicitly by using finally block.in the given program, we are going through the data from a file by using FileReader and we can close it by making use of finally block. In the given program we will read the data of a file by using FileReader and closing it by making use of finally block.

Example

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadData_Demo {
public static void main(String args[]) {
FileReader fr = null;
try {
File file = new File(“file.txt”);
fr
= new FileReader(file); char [] a = new char[50];
fr
.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); // prints the characters one by one
}catch(IOException e) {
e
.printStackTrace();
}finally {
try {
fr
.close();
}catch(IOException ex) {
ex
.printStackTrace();
}
}
}
}

This can also be classify as an automatic resource management, it’s a new exception handling mechanism that wa launched in java 7, that automatically closes the used resources within the try catch block.

For using this statement, user simple required to announce the needed resources within the parenthesis and can create the resource that will get automatically closed at block end. Following given is the try-with-resources statement.

Syntax
try(FileReader fr = new FileReader(“file path”)) {
// use the resource
}catch() {
// body of catch
}
}

Below given is the program that reads data in a java file by using try-with-resources statement.

Example

import java.io.FileReader;
import java.io.IOException;
public class Try_withDemo {
public static void main(String args[]) {
try(FileReader fr = new FileReader(“E://file.txt”)) {
char [] a = new char[50];
fr
.read(a); // reads the contentto the array
for(char c : a)
System.out.print(c); // prints the characters one by one
}catch(IOException e) {
e
.printStackTrace();
}
}
}