0% found this document useful (0 votes)
1 views5 pages

Throws

The document explains the 'throws' keyword in Java, which is used to declare exceptions and inform the caller method to handle them. It provides examples of how to implement exception handling using 'throws' and 'try-catch' blocks, highlighting the consequences of unhandled exceptions. The document also emphasizes that 'throws' does not handle exceptions but rather indicates that an exception may occur, requiring the caller to manage it appropriately.

Uploaded by

kushsevak9
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)
1 views5 pages

Throws

The document explains the 'throws' keyword in Java, which is used to declare exceptions and inform the caller method to handle them. It provides examples of how to implement exception handling using 'throws' and 'try-catch' blocks, highlighting the consequences of unhandled exceptions. The document also emphasizes that 'throws' does not handle exceptions but rather indicates that an exception may occur, requiring the caller to manage it appropriately.

Uploaded by

kushsevak9
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/ 5

Throws Keyword

 Throws Keyword is used to declare an exception.


 It gives an information to caller method that there may occur an exception, so it is
better for the caller method to provide the exception handling code, so that normal flow
of program can be maintained.
 Throws keyword never handle exception
 Exception Handling is mainly used to handle the checked exceptions. If there occurs
any unchecked exception such as NullPointerException, it is programmers' fault that
he is not checking the code before it being used.
 Throws is used with method not inside the method
*****Throws Keyword Program Step by step execution*****

import java.io.FileNotFoundException;
import java.io.FileReader;

//(1)
public class Read
{
public static void main(String[] args)
{
FileReader fr=new FileReader("D:/ab.txt"); //Reading A file
System.out.println("hello");
}
}
CompileTime Error
Read.java:9: error: unreported exception FileNotFoundException; must be caught or
declared to be thrown
FileReader fr=new FileReader("D:/ab.txt");
^

import java.io.FileNotFoundException;
import java.io.FileReader;

//(2)
public class Read
{
public static void main(String[] args)throws FileNotFoundException
{
FileReader fr=new FileReader("D:/ab.txt");
System.out.println("hello");
}
}
Exception in thread "main" java.io.FileNotFoundException: D:\ab.txt (The system
cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
at Read.main(Read.java:9)

Note:
 Abnormal Termination of program .
 “Hello” not printed .
 Exception Object provided by main method to JVM.
 Exception Not handled By Throws keyword

import java.io.FileNotFoundException;
import java.io.FileReader;
//(3) Exception Handling Using Try Catch
public class Read
{
public static void main(String[] args)throws FileNotFoundException
{
try
{
FileReader fr=new FileReader("D:/ab.txt");
}
catch(FileNotFoundException e)
{
System.out.println(e);
System.out.println("hello");
}
}
}

java.io.FileNotFoundException: D:\ab.txt (The system cannot find the file specified)


hello

******Throws Keyword Program Step by step execution through caller method*****


import java.io.FileNotFoundException;
import java.io.FileReader;
//(1)
public class Read
{
void read()
{
FileReader fr=new FileReader("D:/ab.txt");
System.out.println("hello");
}
}
class ReadFile
{
public static void main(String[] args)
{
Read r=new Read();
r.read();
}
}
//Compiletime Error
Read.java:8: error: unreported exception FileNotFoundException; must be caught or
declared to be thrown
FileReader fr=new FileReader("D:/ab.txt");
^
import java.io.FileNotFoundException;
import java.io.FileReader;
//(2)
public class Read
{
void read()throws FileNotFoundException
{
FileReader fr=new FileReader("D:/ab.txt");
System.out.println("hello");
}
}
class ReadFile
{
public static void main(String[] args)
{
Read r=new Read();
r.read();
System.out.println("Exception Handled");
}
}
//Compiletime Error
Read.java:17: error: unreported exception FileNotFoundException; must be caught or
declared to be thrown
r.read();

import java.io.FileNotFoundException;
import java.io.FileReader;
//(3)
public class Read
{
void read()throws FileNotFoundException
{
FileReader fr=new FileReader("D:/ab.txt");
System.out.println("hello");
}
}
class ReadFile
{
public static void main(String[] args)throws FileNotFoundException
{
Read r=new Read();
r.read();
System.out.println("Exception Handled");
}
}

Exception in thread "main" java.io.FileNotFoundException: D:\ab.txt (The system


cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
at Read.read(Read.java:8)
at ReadFile.main(Read.java:17)

 Abnormal Termination of program .


 “Exception Handled” not printed .
 Exception Object provided by main method to JVM.
 Exception Not handled By Throws keyword

import java.io.FileNotFoundException;
import java.io.FileReader;
//(4)
public class Read
{
void read()throws FileNotFoundException
{
FileReader fr=new FileReader("D:/ab.txt");
System.out.println("hello");
}
}
class ReadFile
{
public static void main(String[] args)//Not Recomended to use throws Here
{
Read r=new Read();
try
{
r.read();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}

System.out.println("Exception Handled");
}
}

java.io.FileNotFoundException: D:\ab.txt (The system cannot find the file specified)


Exception Handled

**After providing the file ab.txt

Output
hello
Exception Handled

You might also like