Exception handling in Java - ProgrammerTech
Language:

Search

Exception handling in Java

  • Share this:
Exception handling in Java

Exception in Java

Exceptions in the Java language, are unexpected errors that occur by the programmer or the user, which leads to the program stopping abnormally. There are two types of errors, and they are an error made by the programmer and an error made by the user. The programmer is the one who writes and builds the program, and the user is the one who uses the program and enters the data. The error that the programmer makes when writing the code is taken over by the programmer and fixed. The appearance of an error while using the application and leads to the application stopping is a very bad thing. Because this leads to the users' alienation from the application and their unwillingness to return to using it again.

Our goal in this lesson is the error that the user makes, which is when entering data, he may enter wrong data, which leads to the program being suspended, irritated, and suspended from work. Therefore, the programmer must anticipate and troubleshoot these errors. For example, when entering, the user may enter. Text data in place of numerical data and this is completely unacceptable because numbers only accept numerical data. Or, when dividing by a zero value, the user is dividing by zero, and an error occurs. Or the user enters too much data, which leads to an error called Overflow. These errors are expected by the programmer.

 

Reasons why an exception occurs in Java

  1. In the event that the user is connected to the network and suddenly disconnects from it.
  2. If the program is reading the data of a text file and this text file does not exist.
  3. In the event that the user enters a value that does not match the type of variable in which the value will be stored.
  4. In the event that this user enters a number for an index that is not in the array or a text variable.
  5. If the user divides by a zero value.

How do we handle user errors in Java?

The answer is that we handle this type of error with something called try and catch. The user's error is placed between try and catch in order to ignore this error or phish it and prevent the program from suspending and getting out of work.

The general syntax of try and catch in Java

try{
Commands that we expect to go wrong
}catch(Exception type){
 Commands that are executed if an exception is caught
}

In the try area we put the code in which we suspect that there will be problems or an exception. As for catch, its task is to print a message showing the type of this exception, and the benefit of that is that try - catch will. It catches the error and prevents it from causing a hang or agitation in the program.

Example

package lessontry;
import java.util.Scanner;
public class Lessontry {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int x1,x2,x3;
        x1=s.nextInt();
        x2=s.nextInt();
        x3=x1/x2;
        System.out.println(x3);
        }
    }

When we run this program if we do a normal division then no error will occur. If we divide by 0, an error will appear and the user will suspend the program and exit the program. To avoid this problem, we must catch and anticipate these errors and put them in try and catch to avoid. These problems, to avoid the problem that occurred in the previous example, are as follows.

package lessontry;
import java.util.Scanner;
public class Lessontry {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        try{
        int x1,x2,x3;
        x1=s.nextInt();
        x2=s.nextInt();
        x3=x1/x2;
        System.out.println(x3);
        }catch(Exception e) {System.out.println(e.getMessage());}
    }
}

In this way, we have avoided the problem, and when the user enters a value that is divided by 0, a message will appear to him that you have entered a wrong value, which is 0, that is, it is not divided by zero in the world of mathematics.

Any code that you expect the user to fall into sometimes put it between try and catch to avoid problems

 

How to put more than one catch in a java program?

Sometimes the program encounters more than one error process, then we have to catch all the errors to ensure that no error occurs in the program.

try {
 Program commands that may cause errors
}
catch (Exception1 e1){
  Commands that are executed in the event of an exception
}
catch (Exception2 e2){
  Commands that are executed in the event of an exception
}

We can put as many catch clauses into the program as we expect to see errors.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com