Java Tutorial In-Depth Part-19 |2021|Java throw keyword| java throw vs throws| java throw and throws

///Java Tutorial In-Depth Part-19 |2021|Java throw keyword| java throw vs throws| java throw and throws

Java Tutorial In-Depth Part-19 |2021|Java throw keyword| java throw vs throws| java throw and throws

FavoriteLoadingAdd to favorites

Java tutorial for beginners in depth full course all topics with real time examples
Hello all this is part-19 of 20 you can find the links to other parts below

Java468x60

Full play list

part-1

part-2

part-3

part-4

part-5

part-6

part-7

part-8

part-9

part-10

part-11

part-12

part-13

part-14

part-15

part-16

part-17

part-18

part-19

part-20

throw, throws

the throw keyword. It is used to wantedly throw an exception.
To throw an exception we have to create an object of any exception specific class.

NullPointerException e1 = new NullPointerException();
throw e1;

public void withdraw(int actNum, int amount) {
if (amount availableBalance) {
InsufficientFundsException ex1 = new InsufficientFundsException();
throw ex1;

throw new InsufficientFundsException();
}
}

String filename =”

try {
FileReader fr = new FileReader(filename);
} catch(….) {

}

int i =10
int j = 0
int k = i/j

Checked Exception. These are the classes which are either IMMEDIATE subclasses of the “Exception” class or the child classes of those immediate classes at an lower level hierarchy (except a class named RuntimeException and its hierarchical subclasses at any level of depth)

public class Exception { }

public class Test1 extends Exception { }

public class Test2 extends Test1 { }

public class Test3 extends RuntimeExcepion { }

In case of checked exceptions the compiler forces us to definitely use the try. Catch block code to handle uncontrollable situations such as reading a file, opening a file…

In case of un checked exceptions the compiler does not care or does not force us to use the try.. catch block of code where the problems are under the control of the programmer eg1. / zero, invalid array index.

The throws : it is used to indicate the caller of a specific method (m1) , that the “called method (m2)” causes/raises some exception.
public void static main(…) {

source

By |2021-05-14T13:57:42+00:00May 14th, 2021|Java Video Tutorials|0 Comments

Leave A Comment

*