Exception Handling
There are generally 3 types of errors: Syntax errors, Logical errors and runtime errors
If there is a program, there are generally 2 people associated with the program, one is your developer/programmer and the second is the user, who uses that program. so mostly the first two errors( syntax errors and logical errors) are faced by a programmer but the last one is a runtime error which is faced by the user.
1. Syntax errors: grammatical or coding syntax error
int x,y;
z=x+y;
Here the variable z isnt declared hence its syntax error --> removed by compiler
2. Logical errors: mostly done by the progrmmer by writing false logic
ex-> r=-b/2*a;
instead r=-b/(2*a);
it is removed by tracing back the code or debugger
3. Runtime errors: faced by the users due to which the program doesn't work
For example -> A program asking for the input of age and the user giving input of -10, so the program won't work
as it wasn't the fault of the programmer or developer, it went with the wrong input by the user
or external factors, suppose, you have a car and it doesn't have fuel, it won't run, whose fault it is, companies or owners? , owners fault!. hence, the programme should display some a message that your input is invalid, unavailability of resources like, no internet or file not found.
so runtime errors are removed by -->exception handling
Exception handling is basically done by the programmers in order to remove the runtime errors done by the users due to some invalid input or unavailability of the resources.
Below is an example of how to create an exception class of your own choice
While in software dev we come across a lot of different types of scenarios where there can be several different types of runtime exception or error, so in those cases, we use a generic exception to catch them and print out the reason, that is ->
import java.util.*;
public class except2 {
public static void main(String[] args) {
int a,b,c;
Scanner in=new Scanner(System.in);
a=in.nextInt();
b=in.nextInt();
try{
c=a/b;
System.out.println(c);
}
catch(Exception e){
System.out.println(e);
}
System.out.println("bye");
}
}
We will cover more of its usages in the upcoming series of software development, where there will be a lot more different case scenarios for exception handling. So stay tuned with us 😉
If you have any doubts you can ask the questions directly by commenting down below. Also, don’t forget to read our 3rd article on the OOPs to understand it completely: Understanding OOPs: The third step towards Software Dev
Get the latest tech news and updates, ethical hacking tutorials and cybersecurity tips and tricks. Check out MeuSec for more.
Sometimes we include links to online retail stores and/or online campaigns. If you click on one and make a purchase we may receive a small commission.
Comments: