0% found this document useful (0 votes)
7 views3 pages

Alevel 2 Java 16APR AB

Uploaded by

bhoyepooja93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Alevel 2 Java 16APR AB

Uploaded by

bhoyepooja93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

NIELIT Gorakhpur

Course Name: A Level (2nd Sem) Subject: JAVA


Topic :throw and throws keyword in java Date: 16-04-20

Java throw keyword:


In Java exception handling, throw keyword is used to explicitly throw an
exception from a method or constructor. We can throw either checked or uncheked
exception in java by throw keyword. The throw keyword is mainly used to throw
custom exception. Only object of Throwable class or its sub classes can be thrown.
Program execution stops on encountering throw statement, and the closest catch
statement is checked for matching type of exception.

Syntax:
throw ThrowableInstance

Example:
class Throw1
{
static void validateMarks(int marks)
{
if (marks <80)
throw new ArithmeticException("Not Oracle certified");
else
System.out.println("Oracle certified ");
}

public static void main(String args[])


{
validateMarks (86);
System.out.println("welcome ...");
}
}

Explaination:
In this example, we have created the validateMarks () method that takes integer
value as a parameter. If the marks is less than 80, we are throwing the
ArithmeticException otherwise print a message that you are Oracle certified.
Java throws keyword:
Any method that is capable of causing exceptions must list all the exceptions
possible during its execution, so that programmer calling that method gets a prior
knowledge about which exceptions are to be handled. A method can do so by using
the throws keyword.

class Throws1
{
static void check() throws ArithmeticException
{
System.out.println("Inside check function");
throw new ArithmeticException("demo");
}

public static void main(String args[])


{
try
{
check();
}
catch(ArithmeticException e)
{
System.out.println(e);
}
}
}

2nd example of throws keyword


import java.io.*;
class file1{
public static void main(String[] args) throws IOException{
FileWriter file = new FileWriter("c:\\a.txt");
file.write("NIELIT");
file.close();
}
}

Explaination:
In this example the programmer makes a prior announcement that the code may be
risky so throws exception with the method itself.The above code will throw an
exception if the above mentioned file is not created first or gets deleted.
The difference between throw and throws

throw throws

throw keyword is used to throw an throws keyword is used to declare an


exception explicitly. exception possible during its execution.

throw keyword is followed by an throws keyword is followed by one or more


instance of Throwable class or one of its Exception class names separated by
sub-classes. commas.

throw keyword is declared inside a throws keyword is used with method


method body. signature (method declaration).

We cannot throw multiple exceptions We can declare multiple exceptions


using throw keyword. (separated by commas) using throws
keyword.

Example: Example:

throw new IOException("can not open throws IOException,


File"); ArrayIndexBoundException;

Exercise:
1. Why throw kewword is necessary?Explain with proper example.
2. Explain the usage of throws keyword with example.
3. Write down the differences between throw and throws keyword.

You might also like