TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Exception handling in Java

Overview

In this post, we will discuss single exception handling in Java. Errors occur in java programs as in most things in real life. Also, when an error occurs in a running Java program we say that an exception has occurred. Furthermore, when this happens an Exception object is created and “thrown”. This can be caught or not, depending on the kind of Exception it is and on the program.

try/catch block

It is easy to generate an exception as shown in the below code snippet.The below code will generate a NullPointerException which will be thrown. Also, we can catch this exception and therefore prevent the program from abnormal terminating. To catch an exception if it arises you include the statements that might throw the exception in a try block. Furthermore, the program will continue with the rest of the code after handling the exception.

 

public class ExceptionExample {

    public static void main(String[] args) {
        // ExceptionExample
        try
        {     
            throw new NullPointerException() ;
        }
        catch(NullPointerException npe)
        {    
            npe.printStackTrace();
            
        }
    }

}

 

When a try block is specified, we need to either specify the catch handler or the finally block.

 

Exception_Handling_in_Java1
When we catch an exception we name the class of the exception and give a variable that will have that type. Within the body of the catch clause we can send the Exception object messages. Also, when an exception is thrown in a try block, the execution of the try is abandoned immediately and the system searches for a handler in the catch block of the type of exception that was thrown.

finally block

Java finally block is a block that is used to execute important clean-up code such as closing a connection, browser driver instance, etc. Also,  finally block is always executed regardless of exception is thrown or not as shown in the below picture.

 

Exception_Handling_in_Java2

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Java Platform Overview› Regular Expression in Java

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com