should I always know the type of my exception before I catch it?

Barnabe

For example, is it considered bad practice to write:

try
{
  #some code
}
catch (Exception e){
  #some code
}

Rather than

try
{
  #some code
}
catch (ExceptionName e){  #like ArrayIndexOutOfBoundsException
  #some code
}

I guess the question holds for pretty much every language, be it Python, C++, Java...Any thoughts?

I am asking because it seems to me that you shouldn't, since it means you don't know what kind of error you are handling and what to do with it, but I see some people do it.

Nathan Merrill

YES!

There is a single exception to this:

You want to catch, do something, then re-throw. Maybe you have some resources you need to close before the exception goes up, or you want to inform the user something wrong has happened:

FileResource fileResource = new FileResource("/some/path");
try {
    fileResource.open();
    fileResource.dostuff();
    //other logic
} catch (Exception e){
    fileResource.close();
    throw e;
}

Hence, when an exception gets thrown, you can close your resources before the program exits.

Always catch the most specific exception type you can.

Also, catching all Exceptions is exceptionally bad in Python, because you can catch syntax exceptions and the like.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

should I always know the type of my exception before I catch it?

From Dev

Should I catch and wrap general Exception?

From Dev

Which way should I catch an exception?

From Dev

Which way should I catch an exception?

From Dev

What should I know about networking before I use rsync?

From Dev

Should I always create my DynamoDB tables using hash and range primary key type?

From Dev

What should I know in JavaScript before learning AngularJS?

From Dev

what should I know before merging javascript files into one?

From Dev

Thing i should know before starting apache solr?

From Dev

Class methods: should I always check variables before accessing them?

From Dev

Class methods: should I always check variables before accessing them?

From Dev

When error handling in Java, should I catch Exception to catch all exceptions or individually catch exceptions?

From Dev

Should I always use Lambda Expressions for Exception Tests?

From Dev

Should I always use Lambda Expressions for Exception Tests?

From Dev

I want to know why my 'login logic' always false condition?

From Dev

Should I always removeEventListener?

From Dev

In idiomatic Typescript, should I always declare a variable's type, or should I rely more on type inference?

From Dev

Should I always implement the dispose pattern if my class defines a field in which the field’s type implements the dispose pattern? c#

From Dev

I wrote a project with tornado, but this exception is always in my log file

From Dev

Should I initialize my variables to null before using them?

From Dev

Why should I sign my apk before releasing to PlayStore?

From Dev

Should I turn .CutCopyMode back on before exiting my sub procedure?

From Dev

Should I obfuscate my plugin before sharing with others?

From Dev

Why should I sign my apk before releasing to PlayStore?

From Dev

Should I format my new harddrive before installing linux?

From Dev

What should happen if I throw an exception in my Subscribe callback for an Observable?

From Dev

Where should I put my exception handling in Spring MVC?

From Dev

Should I always call ContainsData before I get data from the Clipboard?

From Dev

How to catch I/O exception

Related Related

  1. 1

    should I always know the type of my exception before I catch it?

  2. 2

    Should I catch and wrap general Exception?

  3. 3

    Which way should I catch an exception?

  4. 4

    Which way should I catch an exception?

  5. 5

    What should I know about networking before I use rsync?

  6. 6

    Should I always create my DynamoDB tables using hash and range primary key type?

  7. 7

    What should I know in JavaScript before learning AngularJS?

  8. 8

    what should I know before merging javascript files into one?

  9. 9

    Thing i should know before starting apache solr?

  10. 10

    Class methods: should I always check variables before accessing them?

  11. 11

    Class methods: should I always check variables before accessing them?

  12. 12

    When error handling in Java, should I catch Exception to catch all exceptions or individually catch exceptions?

  13. 13

    Should I always use Lambda Expressions for Exception Tests?

  14. 14

    Should I always use Lambda Expressions for Exception Tests?

  15. 15

    I want to know why my 'login logic' always false condition?

  16. 16

    Should I always removeEventListener?

  17. 17

    In idiomatic Typescript, should I always declare a variable's type, or should I rely more on type inference?

  18. 18

    Should I always implement the dispose pattern if my class defines a field in which the field’s type implements the dispose pattern? c#

  19. 19

    I wrote a project with tornado, but this exception is always in my log file

  20. 20

    Should I initialize my variables to null before using them?

  21. 21

    Why should I sign my apk before releasing to PlayStore?

  22. 22

    Should I turn .CutCopyMode back on before exiting my sub procedure?

  23. 23

    Should I obfuscate my plugin before sharing with others?

  24. 24

    Why should I sign my apk before releasing to PlayStore?

  25. 25

    Should I format my new harddrive before installing linux?

  26. 26

    What should happen if I throw an exception in my Subscribe callback for an Observable?

  27. 27

    Where should I put my exception handling in Spring MVC?

  28. 28

    Should I always call ContainsData before I get data from the Clipboard?

  29. 29

    How to catch I/O exception

HotTag

Archive