Giter Club home page Giter Club logo

Comments (4)

PhilippSalvisberg avatar PhilippSalvisberg commented on May 26, 2024

from SonarSource:

"WHEN OTHERS" clauses should be used for exception handling

Ensure that every possible exception is caught by using a WHEN OTHERS clause.

Noncompliant Code Example

SET SERVEROUTPUT ON

DECLARE
  result PLS_INTEGER;
  custom_exception EXCEPTION;
BEGIN
  result := 42 / 0;                            -- "Unexpected" division by 0

  RAISE custom_exception;
EXCEPTION                                      -- Non-Compliant
  WHEN custom_exception THEN
    DBMS_OUTPUT.PUT_LINE ('custom_exception: ' || DBMS_UTILITY.FORMAT_ERROR_STACK);
END;
/

Compliant Solution

SET SERVEROUTPUT ON

DECLARE
  result PLS_INTEGER;
  custom_exception EXCEPTION;
BEGIN
  result := 42 / 0;                            -- "Unexpected" division by 0

  RAISE custom_exception;
EXCEPTION                                      -- Compliant
  WHEN custom_exception THEN
    DBMS_OUTPUT.PUT_LINE ('custom_exception: ' || DBMS_UTILITY.FORMAT_ERROR_STACK);
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE ('other: ' || DBMS_UTILITY.FORMAT_ERROR_STACK);
END;
/

from plsql-and-sql-coding-guidelines.

PhilippSalvisberg avatar PhilippSalvisberg commented on May 26, 2024

I do not agree with this rule. This would require every PL/SQL block to handle exceptions, introducing useless code and is working against the default behaviour.

from plsql-and-sql-coding-guidelines.

silviomarghitola avatar silviomarghitola commented on May 26, 2024

The intention of this rule - from my point of view - is not to handle exceptions in every PL/SQL block, but in case there is an EXCEPTION block, there should always be a WHEN OTHERS statement.

from plsql-and-sql-coding-guidelines.

PhilippSalvisberg avatar PhilippSalvisberg commented on May 26, 2024

I do not think this changes much.

It is good to not include an exception handler for OTHERS when there is not other exception handler. But if you define an other handler you have also to define a handler for OTHERS? - What is the reasoning? How is this improving the code?

I agree that there are cases where it really make sense, e.g when you need to catch all exceptions, handle them and log them all. But making this a common rule is a step too much for me.

from plsql-and-sql-coding-guidelines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.