“Obsługa wyjątków Java” Kod odpowiedzi

Obsługa wyjątków Java za pomocą Try ... Catch

class Main {
  public static void main(String[] args) {

    try {

      // code that generate exception
      int divideByZero = 5 / 0;
      System.out.println("Rest of code in try block");
    }
    
    catch (ArithmeticException e) {
      System.out.println("ArithmeticException => " + e.getMessage());
    }
  }
}
SAMER SAEID

Zalety obsługi wyjątków w Javie

1) Separating normal code from exception handling code to avoid abnormal 
termination of program.
2) Categorizing in to different types of Exceptions so that rather than 
handling all exceptions with Exception root class we can handle with specific 
exceptions. It is recommended to handle exceptions with specific Exception 
instead of handling with Exception root class.
3) Call stack mechanism : If a method throws an exception and it is not handled 
immediately, then that exception is propagated or thrown to the caller of that 
method. This propogation continues till it finds an appropriate exception 
handler,if it finds handler it would be handled otherwise program terminates
abruptly.
Thankful Tuatara

Wyjątki Java - spróbuj ... Catch

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.");
    }
  }
}
naly moslih

Wyjątek Java

    - ArithmeticException 
    It is thrown when an exceptional condition has occurred in an arithmetic operation.
    
    - ArrayIndexOutOfBoundsException 
    It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
    
    - ClassNotFoundException 
    This Exception is raised when we try to access a class whose definition is not found
    
    - FileNotFoundException 
    This Exception is raised when a file is not accessible or does not open.
    
    - IOException 
    It is thrown when an input-output operation failed or interrupted
    
    - InterruptedException 
    It is thrown when a thread is waiting, sleeping, or doing some processing, and it is interrupted.
    
    - NoSuchFieldException 
    It is thrown when a class does not contain the field (or variable) specified
    
    - NoSuchMethodException 
    It is thrown when accessing a method which is not found.
    
    - NullPointerException 
    This exception is raised when referring to the members of a null object. Null represents nothing
    
    - NumberFormatException 
    This exception is raised when a method could not convert a string into a numeric format.
    
    - RuntimeException 
    This represents any exception which occurs during runtime.
    
    - StringIndexOutOfBoundsException 
    It is thrown by String class methods to indicate that an index is either negative or greater than the size of the string
Strange Starling

Obsługa wyjątków w Javie

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...");  
  }  
}  
NajmAdin

Obsługa wyjątków Java

try {
    System.out.println("I am in try block");
} catch(Exception ex){
    ex.printStackTrace();
} finally {
    System.out.println("I am in finally block");
}
Precious Pigeon

Odpowiedzi podobne do “Obsługa wyjątków Java”

Pytania podobne do “Obsługa wyjątków Java”

Więcej pokrewnych odpowiedzi na “Obsługa wyjątków Java” w Java

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu