Commit f1fd563d authored by Bhargavi Ghanta's avatar Bhargavi Ghanta

Task 9 as completed: User input validation implemented

parents 62452d2a 16c67723
...@@ -2,7 +2,15 @@ ...@@ -2,7 +2,15 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<<<<<<< HEAD
<module fileurl="file://$PROJECT_DIR$/ValidationFramework.iml" filepath="$PROJECT_DIR$/ValidationFramework.iml" /> <module fileurl="file://$PROJECT_DIR$/ValidationFramework.iml" filepath="$PROJECT_DIR$/ValidationFramework.iml" />
=======
<<<<<<< HEAD
<module fileurl="file://$PROJECT_DIR$/RetryUtility.iml" filepath="$PROJECT_DIR$/RetryUtility.iml" />
=======
<module fileurl="file://$PROJECT_DIR$/GlobalExceptionHandler.iml" filepath="$PROJECT_DIR$/GlobalExceptionHandler.iml" />
>>>>>>> 1954dd751dcf534a5ca8daa53b9ea31178d381eb
>>>>>>> 16c677232062e76314ab089fe3a169732c4e4356
</modules> </modules>
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -2,6 +2,5 @@ ...@@ -2,6 +2,5 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" /> <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component> </component>
</project> </project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
----- Exception Occurred -----
Thread: main
Exception: java.lang.ArithmeticException: / by zero
at GlobalExceptionLogger.divide(GlobalExceptionLogger.java:23)
at GlobalExceptionLogger.main(GlobalExceptionLogger.java:16)
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class GlobalExceptionLogger {
public static void main(String[] args) {
// Set up global exception handler
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
logException(throwable);
});
// Sample code that throws an unhandled exception
System.out.println("Program started.");
int result = divide(10, 0); // This will cause ArithmeticException
System.out.println("Result: " + result);
}
// Method that may throw an exception
public static int divide(int a, int b) {
return a / b; // division by zero triggers an exception
}
// Method to log exceptions to a file
public static void logException(Throwable throwable) {
try (PrintWriter writer = new PrintWriter(new FileWriter("error.log", true))) {
writer.println("----- Exception Occurred -----");
writer.println("Thread: " + Thread.currentThread().getName());
writer.println("Exception: " + throwable);
for (StackTraceElement element : throwable.getStackTrace()) {
writer.println("\tat " + element);
}
writer.println(); // Blank line for separation
} catch (IOException e) {
System.err.println("Failed to log exception: " + e.getMessage());
}
}
}
import java.util.concurrent.Callable;
public class RetryUtility {
/**
* Retries a task up to maxRetries with exponential backoff.
*
* @param task The task to execute.
* @param maxRetries Number of retry attempts.
* @param initialDelayMillis Initial delay before retrying (in milliseconds).
* @return The result of the task if successful.
* @throws Exception If all retry attempts fail.
*/
public static <T> T retry(Callable<T> task, int maxRetries, long initialDelayMillis) throws Exception {
int attempt = 0;
while (true) {
try {
return task.call(); // Attempt the task
} catch (Exception e) {
attempt++;
if (attempt > maxRetries) {
throw new Exception("All retry attempts failed after " + maxRetries + " tries", e);
}
long backoffTime = initialDelayMillis * (1L << (attempt - 1)); // Exponential backoff
System.out.println("Attempt " + attempt + " failed: " + e.getMessage());
System.out.println("Retrying in " + backoffTime + "ms...");
Thread.sleep(backoffTime);
}
}
}
public static void main(String[] args) {
try {
String result = retry(() -> {
// Simulate an unreliable remote service
if (Math.random() < 0.7) {
throw new RuntimeException("Network error!");
}
return "Success!";
}, 5, 1000); // Try up to 5 times, starting with 1 second backoff
System.out.println("Final Result: " + result);
} catch (Exception e) {
System.err.println("Operation failed after retries: " + e.getMessage());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment