Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java Throw keyword
3.
Checked Exception
3.1.
Example
4.
Unchecked Exception
4.1.
Example
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Mar 27, 2024

Throw keyword

Introduction

In Java, exceptions allow writing good quality code where the errors are checked at the compile-time instead of runtime, making code debugging more manageable.

Also read, Duck Number in Java And Hashcode Method in Java

Java Throw keyword

The throw keyword is used to throw a customised error. We specifically mention the exception object which is to be thrown. The Exception comes with some block describing the error. These exceptions may be related to user input, server, etc. There are many exception types in Java which are as follows:

  • ArithmeticException
  • ClassNotFoundException
  • ArrayIndexOutOffBoundException
  • SecurityException

Syntax of a throw keyword

throw new exception_class("error message");
You can also try this code with Online Java Compiler
Run Code


Also see,  Swap Function in Java

Checked Exception

Checked exceptions are particular types of exceptions that are checked at compile time. This means that if a method is throwing an exception, it should be handled using a try-catch block, or this can be operated using the throws keyword. The code will show a compilation error if this is not dealt with.

Example

Code

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


public class Ex12  {


public static void method() throws FileNotFoundException {  
  
        FileReader file = new FileReader("C:\\Users\\MS\\Desktop\\answer.docx");  
        BufferedReader fileInput = new BufferedReader(file);  
  
      
        throw new FileNotFoundException();  
      
    }  
    //main method  
    public static void main(String args[]){  
        try  
        {  
            method();  
        }   
        catch (FileNotFoundException e)   
        {  
            e.printStackTrace();  
        }  
        System.out.println("rest of the code...");    
  }    
}    
You can also try this code with Online Java Compiler
Run Code

Output

java.io.FileNotFoundException
at CODING/CN.Ex12.method(Ex12.java:15)
at CODING/CN.Ex12.main(Ex12.java:22)
rest of the code...
You can also try this code with Online Java Compiler
Run Code

In this code, we have created two separate functions one is for throwing an exception and the other for catching an exception. In the first method, we simply used the file reader function to fetch some files after we read that file and then throw some exceptions. The next function uses a try-catch exception to catch that function. If we found the file, it will print the stack trace with the help of a StackTrace function. Then it will move again as it is.

Must Read Static Blocks In Java.

Unchecked Exception

Unchecked Exceptions are those which are not checked during compile time. The compiler unchecks all types of exceptions. Its the duty of the programmer to specify the catch exception or specify it. In Java exceptions under Error and RunTimeException classes, you can find the unchecked exceptions.  

Example

Code

public class Code1 {
  
    // Main driver method
    public static void main(String args[])
    {
        int x = 0;
        int y = 10;
        int z = y / x;
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Exception in thread "main" java.lang.ArithmeticException: / by zero
at Code1.main(Code1.java:8)

When run, the given program throws ArithmeticException. Since ArithmeticException is an unchecked exception, the compiler allowes it to compile.

You can practice by yourself with the help of online java compiler.

Frequently Asked Questions

  1. What is the use of the throw keyword?
    The throw keyword is used to throw an explicitly single exception. It can be expressed in the form of a method and also in the form of a block of code. In this case, both checked and unchecked exceptions can be thrown using the throw keyword.
     
  2. What is an exception list?
    It is a part of the logical message tree in which the message flow writes information about the exceptions that might occur during the processing of the message. 

Key Takeaways

In this blog, we have covered the throw keyword. We have covered a brief introduction of the throw keyword, a Java throw keyword along with its syntax, and the use of the java throw keyword. We also took a few examples of using the throw keyword in different scenarios.

If you want to learn more about exception handling, you can visit our website, Exception Handling.

Live masterclass