xxxxxxxxxx
import java.io.BufferedReader;
import java.io.IOException;
// Define a custom exception class
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
// Define an interface
interface MyInterface {
void myMethod(int age) throws CustomException, IOException;
}
// Implement a class that uses the custom exception and implements the interface
class MyClass implements MyInterface {
@Override
public void myMethod(int age) throws CustomException, IOException {
BufferedReader reader = null;
try {
// Some logic that may throw custom exception or IOException
if (age < 0) {
throw new CustomException("CustomException: Age cannot be negative!");
}
// ... rest of the method's logic ...
} catch (CustomException ce) {
// Log and rethrow CustomException
System.err.println("Caught a custom exception: " + ce.getMessage());
ce.printStackTrace();
throw ce;
} catch (IOException io) {
// Log and rethrow IOException
System.err.println("Caught an IOException: " + io.getMessage());
io.printStackTrace();
throw io;
} catch (Exception e) {
// Log and rethrow generic Exception
System.err.println("Caught a generic exception: " + e.getMessage());
e.printStackTrace();
throw e;
} finally {
// Close resources in the finally block
if (reader != null) {
try {
reader.close();
System.out.println("BufferedReader closed in the finally block");
} catch (IOException e) {
System.err.println("Exception while closing BufferedReader: " + e.getMessage());
e.printStackTrace();
}
}
}
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Instantiate and use the class
MyClass myInstance = new MyClass();
try {
myInstance.myMethod(-5); // Example with a negative age
} catch (CustomException | IOException | Exception e) {
// Handle the exception if needed
System.out.println("Exception caught in the main method: " + e.getMessage());
e.printStackTrace();
}
}
}
xxxxxxxxxx
public class MyClass {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3, 4, 5, 6};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong. check again");
}
}
}
xxxxxxxxxx
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
xxxxxxxxxx
try {
// Code to try, which is throwing an Exception, e.g.
/*example*/ Thread.sleep(100)
} catch (InterruptedException e /*Or any other exception*/) {
// Handle Exception, usually:
e.printStackTrace(); // Print the StackTrace of the exception to see what cause it
} finally {
// Code executed after try / catch, used to close streams
/*example*/ in.close();
}
xxxxxxxxxx
int midterm;
System.out.printLn("Enter midterm grade");
do
{
try {
string s = in.nextLine();
midterm = Integer.parseInt(s);
break;
}
catch (Exception e)
{
System.out.printLn("Couldn't parse input, please try again");
}
}
while (true);
xxxxxxxxxx
import java.io.*;
import java.util.Scanner;
public class Test {
public static void main(String args[]) throws Exception {
String input;
int ch1;
float ch2;
String ch3;
Scanner one = new Scanner(System.in);
input = one.nextLine();
try {
ch1 = Integer.parseInt(input);
System.out.println("integer");
return;
} catch (NumberFormatException e) {
}
try {
ch2 = Float.parseFloat(input);
System.out.println("float");
return;
} catch (NumberFormatException e) {
}
try {
ch3 = String.valueOf(input);
System.out.println("String");
} catch (NumberFormatException e) {
}
}
}
xxxxxxxxxx
try {
// code that might throw an exception
} catch (ExceptionType e) {
// code to handle exception
} finally {
// code that will always execute
}
xxxxxxxxxx
try{
//Code that is checked for error
} catch(Exception e){
//Code runs when error is caught
}