Skip to content Skip to sidebar Skip to footer

How To Handle Unexpected Error And Uncaught Exceptions

I use 'try catch' to handle some expected error, but sometime, there will be unexpected error. i mean if i don't use 'try catch' , how to handle crash error ,and send error messa

Solution 1:

A try catch will also be able to catch unexpected errors. You just have to put them in the appropriate order: the most specific first, then more general. An example (it's general Java, no specific Android code, but you'll get the point):

    try {
        int input = sc.nextInt();  //random number
        int amount = 10 / input;   // not every result can be stored in an int
    } catch (InputMismatchException ime) {
        // catches error of non-numeric value
        System.out.println("input is not a valid number");
    } catch (ArithmeticException ae) {
        //catches error of division by 0
        System.out.println("Division by 0!");
    }
    catch(Exception ex)
    {
        // catches any other error
        System.out.println("other error");
    }

Post a Comment for "How To Handle Unexpected Error And Uncaught Exceptions"