Saturday, December 6, 2025
HomeLanguagesJavaLogRecord setThrown() method in Java with Examples

LogRecord setThrown() method in Java with Examples

The setThrown() method of java.util.logging.LogRecord is used to a throwable associated with the log event.This is used to log Exceptions in the logRecord that can be used for logging messages.

Syntax:

public void setThrown(Throwable thrown)

Parameters: This method accepts thrown as a parameter which is a throwable object. It can be null also.

Return: This method returns nothing.

Below programs illustrate setThrown() method:
Program 1:




// Java program to illustrate setThrown() method
  
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogRecord;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create LogRecord object
        LogRecord logRecord
            = new LogRecord(Level.SEVERE,
                            "Hello Logger");
  
        // set throwable object
        logRecord.setThrown(
            new IOException(
                "Error in Input"));
  
        // print the method name
        System.out.println(
            "throwable object Message = "
            + logRecord.getThrown()
                  .toString());
    }
}


Output:

throwable object Message = java.io.IOException: Error in Input

Program 2:




// Java program to illustrate setThrown() method
  
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogRecord;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create LogRecord object
        LogRecord logRecord
            = new LogRecord(Level.SEVERE,
                            "Hello Logger");
  
        // create a throwable object
        Exception exception
            = new ArithmeticException(
                "divide by 0");
  
        // set throwable object
        logRecord.setThrown(exception);
  
        // print the result
        System.out.println(
            "throwable object = "
            + logRecord.getThrown()
                  .toString());
    }
}


Output:

throwable object = java.lang.ArithmeticException: divide by 0

References: https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#setThrown(java.lang.Throwable)

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32427 POSTS0 COMMENTS
Milvus
103 POSTS0 COMMENTS
Nango Kala
6803 POSTS0 COMMENTS
Nicole Veronica
11944 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12014 POSTS0 COMMENTS
Shaida Kate Naidoo
6934 POSTS0 COMMENTS
Ted Musemwa
7188 POSTS0 COMMENTS
Thapelo Manthata
6882 POSTS0 COMMENTS
Umr Jansen
6867 POSTS0 COMMENTS