Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8249093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:25:03+00:00 2026-06-07T23:25:03+00:00

Consider the following Java code: try{ // do something // this piece of code

  • 0

Consider the following Java code:

try{

    // do something
    // this piece of code throws several checked exceptions.

} catch (IllegalArgumentException e) {
    handleException(e);
} catch (IllegalAccessException e) {
    handleException(e);
} catch (InvocationTargetException e) {
    handleException(e);
} catch (InstantiationException e) {
    handleException(e);
} catch (NoSuchMethodException e) {
    handleException(e);
} catch (IOException e) {
    handleException(e);
} catch (NoSuchFieldException e) {
    handleException(e);
}

The code in try block throws several checked exceptions. All I want to do is to log a message when an exception occurs (with some custom message strings). I.e. my exception handling logic is same for all exceptions.

I feel the above code dosn’t look good (more LOC and reduced readability).

Is there any better ways to handle such cases?

The following solution is not a best practice, so not recommended (by Check style).

try{
    // do something very bad
} catch (Exception e) {
    handleException(e);
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:25:04+00:00Added an answer on June 7, 2026 at 11:25 pm

    In Java 6 you don’t have any option much more appealing than what you have already suggested.

    But Java 7 has a multi-catch statement that you can use:

    catch(IllegalArgumentException | IllegalAccessException | IOException exception) {
        handleException(e);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following C++ code: try { throw foo(1); } catch (foo &err) {
I have a question regarding exception handling. Consider following Java code snippet. try{ //code
Consider the following piece of Java code. int N = 10; Object obj[] =
I have question about try, catch and finally in Java. Consider the following scenario:
Consider the following Java code: volatile boolean v1 = false; volatile boolean v2 =
Java noob question: Consider the following C array and initializer code: struct { int
I have a question about how GC works in Java. Consider the following code:
Consider the following code: class Foo(var name: String = bar) Now i try to
Consider the following code: function Robot(weapon) { this.weapon = weapon; this.fire = function() {
Consider the following Java code: Object a = new Integer(2); Object b = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.