Logger throwing() method in Java with Examples
throwing(String sourceClass, String sourceMethod, Throwable thrown) method is used to Log throwing an exception.In many scenario method is closed by throwing an exception then this is a very helpful method to log that a method is terminating by throwing an exception. The logging is done using the FINER level logging. If the level of the logger is set to log FINER level logging then the given arguments are stored in a log record with a message THROW which is forwarded to all registered output handlers.
Syntax:
public void throwing(String sourceClass,
String sourceMethod,
Throwable thrown)
Parameters: This method accepts three parameters:
- sourceClass is the name of the class that issued the logging request,
- sourceMethod is the name of the method and
- result is The Throwable that is being thrown.
Return value: This method returns nothing.
Below programs illustrate throwing(String sourceClass, String sourceMethod, Object result) method:
Program 1:
// Java program to demonstrate // throwing(String, String, Throwable) method import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; public class GFG { public static void main(String[] args) throws SecurityException, IOException { // Create a Logger Logger logger = Logger.getLogger( GFG.class.getName()); // Create a file handler object FileHandler handler = new FileHandler("logs.txt"); handler.setFormatter(new SimpleFormatter()); // Add file handler as // handler of logs logger.addHandler(handler); // set Logger level() logger.setLevel(Level.FINER); // set Logger level() logger.setLevel(Level.FINER); // call throwing method with class // name = GFG and method name = main // and IO Exception as Thrown object logger.throwing(GFG.class.getName(), GFG.class.getMethods()[0].getName(), new IOException()); } } |
The output printed on log.txt is shown below.
Output:

Program 2:
// Java program to demonstrate // throwing(String, String, Throwable) method import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; public class GFG { // Create a Logger static Logger logger = Logger.getLogger( GFG.class.getName()); public static void main(String[] args) throws SecurityException, IOException { // Create a file handler object FileHandler handler = new FileHandler("logs.txt"); // Add file handler as // handler of logs logger.addHandler(handler); // set Logger level() logger.setLevel(Level.FINER); // set Logger level() logger.setLevel(Level.FINER); // call throwing method with string // and ArithmeticException as Thrown object logger.throwing(String.class.getName(), String.class.getMethods()[0].getName(), new ArithmeticException()); } } |
The output printed on log.txt is shown below.
Output:

Recommended Posts:
- Logger log() Method in Java with Examples
- Logger removeHandler() method in Java with Examples
- Logger setResourceBundle() method in Java with Examples
- Logger getHandler() Method in Java with Examples
- Logger exiting() method in Java with Examples
- Logger entering() method in Java with Examples
- Logger addHandler() method in Java with Examples
- Logger fine() method in Java with Examples
- Logger getFilter() Method in Java with Examples
- Logger getLevel() method in Java with Examples
- Logger setUseParentHandlers() method in Java with Examples
- Logger setLevel() method in Java with Examples
- Logger setParent() method in Java with Examples
- Logger getAnonymousLogger() Method in Java with Examples
- Logger config() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



