Exceptions Handling
@startuml
actor User
User--> (file) #line:red;line.bold;text:red : File not found
User--> (database) #line:red;line.bold;text:red : Database unreachable
User--> (application) #line:red;line.bold;text:red : Application error
User--> (network) #line:red;line.bold;text:red : Network error
@enduml
Unanticipated events that interrupt the flow of the execution in a program. It would terminate the program abnormally.
Providing an alternative way
Providing fiendly message to end user
Enough information to Debug the problem
public static void main(String main[]){
System.out.println(j);
}
Commonly made by developers
//The output is -336
public static void main(String main[]){
int i=1000;
byte b=(byte)i;
System.out.println(b*14);
}
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}
public class Main {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}
try {
// code that may throw an exception
} catch (Exception e) {
// code to handle the exception
}
try {
System.out.println(1 / 0);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
System.out.println("End of the program");
Github code File IO
When exception is thrown it must of subclass
throw throwableInstance;
ArithmeticException e = new ArithmeticException("you can't divide by zero");
throw e;
throw new ArithmeticException("you can't divide by zero");
To throw a new instance of a built-in exception
Used inside a method or block of code to explicitly throw an exception.
Inside custom exception
ArithmeticException e=null ;
throw e;
ArithmeticException e = new ArithmeticException("you can't divide by zero");
throw e;
System.out.println("Why i am getting error");
throw new Test();
class Test{}
class Test extends RuntimeException{}
throw new Test();
Github code File IO
String str= null;
int length = str.length(); //throws NullPointerException(Unchecked ex)
int[] arr = new int[5];
int x=array[9]; //throws ArrayIndexOutOfBoundsException(Unchecked ex)
int x=Integer.parseInt("abc"); //throws NumberFormatException(Unchecked ex)
Filereader fr = new FileReader("file.txt"); //throws FileNotFoundException(Checked ex)
public static void main(String[] args) {
FileReader reader = null;
try {
reader = new FileReader("file.txt");
// Code that uses the reader
} catch (IOException e) {
// Exception handling
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// Exception handling for close() method
}
}
}
try (FileReader reader1 = new FileReader("file.txt")) {
// Code that uses the resource
} catch (IOException e) {
// Exception handling
}
// The resource is automatically closed here
}
public static void main(String[] args) {
try (
FileReader reader = new FileReader("input.txt");
FileWriter writer = new FileWriter("output.txt")
) {
// reader = new FileReader("x.txt");
int character;
while ((character = reader.read()) != -1) {
writer.write(character);
}
System.out.println("File copied successfully.");
} catch (IOException e) {
System.out.println("An error occurred while reading or writing the file.");
e.printStackTrace();
}
}
Errors | Exceptions |
---|---|
The error indicates trouble that primarily occurs due to the scarcity of system resources. | The exceptions are the issues that can appear at runtime and compile time. |
It is not possible to recover from an error. | It is possible to recover from an exception. |
In java, all the errors are unchecked. | In java, the exceptions can be both checked and unchecked. |
The system in which the program is running is responsible for errors. | The code of the program is accountable for exceptions. |
They are described in the java.lang.Error package. | They are described in java.lang.Exception package |