Exception in main



  • Why in the method main I need to write. throws Exception? Why would that be? Who will accept the exception?



  • Operator throws shows that under certain conditions, the method may be waived by the processor. try-catch There will be no exception or no.
    So, mainif there are unintended errors in a type code, the method mandatory If there is no exception, the method will end its work in peace. Me throws - it's a warning, which might be an exception in the method.
    Well, to avoid situations of exclusion in the main method. main, doubtful actions should be processed try-catch

    For example, I think you know you can't share zero, that's what this action doesn't make sense. Therefore, it can be assumed that if we split zero, there will be an exception:

    public static void main(String[] args) throws Exception {
        System.out.println(1/0); // Пытаемся делить на нуль
    }
    

    That's the method. main() We got an exception.

    Exception in thread "main" java.lang.ArithmeticException: / by zero

    It may be envisaged that the user might want to share a zero curve, so we'll just work out the exception:

    public static void main(String[] args) throws Exception {
        try {
            System.out.println(1/0); // Пытаемся делить на нуль
        } catch(ArithmeticException exc) { System.out.println("На нуль делить нельзя"); }
    }
    

    Thus the method main() I didn't throw out the exception because the design try-catch I'm sorry. ArithmeticException

    And who's stopping you from using just:

    public static void main(String[] args) {
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2