Wednesday, July 3, 2024
HomeLanguagesJavaLogRecord getThrown() method in Java with Examples

LogRecord getThrown() method in Java with Examples

The getThrown() method of java.lang.reflect.LogRecord is used to get 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 Throwable getThrown()

Parameters: This method accepts nothing.

Return: This method returns a throwable.

Below programs illustrate getThrown() method:
Program 1:




// Java program to illustrate
// getThrown() 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.INFO,
                            "GFG Logger");
        logRecord.setThrown(
            new IOException(
                "Error in Input"));
  
        // get Throwable object
        Throwable throwObj
            = logRecord.getThrown();
  
        // print result
        System.out.println(
            "throwable object = "
            + throwObj
                  .toString());
    }
}


Output:

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

Program 2:




// Java program to illustrate
// getThrown() method
  
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.WARNING,
                            "Logger");
        logRecord.setThrown(
            new ArithmeticException());
  
        // get Throwable object
        Throwable throwObj
            = logRecord.getThrown();
  
        // print result
        System.out.println(
            "throwable object = "
            + throwObj
                  .toString());
    }
}


Output:

throwable object = java.lang.ArithmeticException

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments