“Manejo de excepciones en Java” Código de respuesta

intenta atrapar java

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

Manejo de excepciones de Java usando intento ... 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

Ventajas del manejo de excepciones en Java

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

Excepciones de Java - intente ... atrapar

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

Manejo de excepciones en Java

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

Implementación de clase de excepción en Java

public class JavaExceptionExample extends Exception{  
  public JavaExceptionExample(){
  }
   public JavaExceptionExample(String s){
     //String parameter which is the detail message of the exception.
  }
}  
Lukatic

Respuestas similares a “Manejo de excepciones en Java”

Preguntas similares a “Manejo de excepciones en Java”

Más respuestas relacionadas con “Manejo de excepciones en Java” en Java

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código