- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to handle the exception methods in Java
Problem Description
How to handle the exception methods?
Solution
This example shows how to handle the exception methods by using System.err.println() method of System class.
public class NewClass { public static void main(String[] args) { try { throw new Exception("My Exception"); } catch (Exception e) { System.err.println("Caught Exception"); System.err.println("getMessage():" + e.getMessage()); System.err.println("getLocalizedMessage():" + e.getLocalizedMessage()); System.err.println("toString():" + e); System.err.println("printStackTrace():"); e.printStackTrace(); } } }
Result
The above code sample will produce the following result.
Caught Exception getMassage(): My Exception gatLocalisedMessage(): My Exception toString():java.lang.Exception: My Exception print StackTrace(): java.lang.Exception: My Exception at ExceptionMethods.main(ExceptionMeyhods.java:12)
java_exceptions.htm
Advertisements