xxxxxxxxxx
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
}
}
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
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
An exception is an event, which occurs during the execution of a
program, that disrupts the normal flow of the program's instructions.
xxxxxxxxxx
public class Main {
public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}
xxxxxxxxxx
In java exception is an object. Exceptions are created when an abnormal
situations are arised in our program. Exceptions can be created by JVM or
by our application code. All Exception classes are defined in java.lang.
In otherwords we can say Exception as run time error.
xxxxxxxxxx
public class JavaExceptionExample extends Exception{
public JavaExceptionExample(){
}
public JavaExceptionExample(String s){
//String parameter which is the detail message of the exception.
}
}
xxxxxxxxxx
Exception is an abnormal condition.
There are mainly two types of exceptions: checked and unchecked.
An error is considered as the unchecked exception. However, according to Oracle,
there are three types of exceptions namely:
1. Checked Exception
2. Unchecked Exception
3. Error
xxxxxxxxxx
try {
System.out.println("I am in try block");
} catch(Exception ex){
ex.printStackTrace();
} finally {
System.out.println("I am in finally block");
}