{"record":{"id":"cc3168bffbabbed7","repo":"kunal-kushwaha/DSA-Bootcamp-Java","slug":"please-do-no-divide-by-zero","errorCode":null,"errorMessage":"please do no divide by zero","messagePattern":"please do no divide by zero","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"lectures/17-oop/code/src/com/kunal/exceptionHandling/Main.java","lineNumber":28,"sourceCode":"            String name = \"Kunal\";\n            if (name.equals(\"Kunal\")) {\n                throw new MyException(\"name is kunal\");\n            }\n        } catch (MyException e) {\n            System.out.println(e.getMessage());\n        } catch (ArithmeticException e) {\n            System.out.println(e.getMessage());\n        } catch (Exception e) {\n            System.out.println(\"normal exception\");\n        } finally {\n            System.out.println(\"this will always execute\");\n        }\n\n    }\n\n    static int divide(int a, int b) throws ArithmeticException{\n        if (b == 0) {\n            throw new ArithmeticException(\"please do no divide by zero\");\n        }\n\n        return  a / b;\n    }\n}\n","sourceCodeStart":10,"sourceCodeEnd":34,"githubUrl":"https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c/lectures/17-oop/code/src/com/kunal/exceptionHandling/Main.java#L10-L34","documentation":"The static divide method explicitly throws java.lang.ArithmeticException with a friendly message when the divisor b is 0, guarding the a / b division that would otherwise throw '/ by zero'. The throws ArithmeticException clause is documentation only, since it is unchecked.","triggerScenarios":"Calling divide(a, 0) for any int a; the check is if (b == 0) throw new ArithmeticException(\"please do no divide by zero\").","commonSituations":"Dividing by a runtime-computed value (user input, counts, averages over empty collections); passing defaulted 0 denominators; computing ratios where totals can be zero.","solutions":["Check the divisor before calling: only call divide when b != 0.","Catch ArithmeticException around the call and handle it.","Return a sentinel/Optional or handle zero explicitly in the calling logic."],"exampleFix":"// before\nint r = divide(a, b);\n// after\nif (b != 0) {\n    int r = divide(a, b);\n} else {\n    System.out.println(\"cannot divide by zero\");\n}","handlingStrategy":"validation","validationCode":"if (b != 0) {\n    int r = divide(a, b);\n} else {\n    System.out.println(\"cannot divide by zero\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    int r = divide(a, b);\n} catch (ArithmeticException e) {\n    System.out.println(e.getMessage());\n}","preventionTips":["Always validate divisors are non-zero before division.","Never pass defaulted/uninitialized values as denominators.","When averaging, check the collection is non-empty first.","Catch ArithmeticException at the boundary where user/runtime input enters the calculation."],"tags":["java","arithmeticexception","divide-by-zero"],"backgroundTag":"divide-by-zero","analyzedSha":"6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c","analyzedAt":"2026-08-31T22:04:22.314Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}