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 9108725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:52:26+00:00 2026-06-17T02:52:26+00:00

I am fairly new to java esp. to some of the coding practices. I

  • 0

I am fairly new to java esp. to some of the coding practices. I have a situation where a method throws a NullPointerException and the calling method catches it.

try {
    String test = Class.method(arg)
}    
catch (Exception ex) {
    ...
}

public String method(arg){
String str;
...
if(str == null) throw(exception)
return str;
}

Now some method inside Class.method(arg) throws a NullPointerException and it gets caught in the above catch (like above). Instead I want to do something like:

if (test == null) { do something else }

inside the try block.

What is the best way to handle this? Can I remove the throw inside method and make it return null?

  • 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-17T02:52:27+00:00Added an answer on June 17, 2026 at 2:52 am

    You should catch Exception in method(arg) and return null incase of any exception.

    Sample Program:

    public class Test1 {
    
        public static void main(String[] args) {
            try{
                String test = method("1");
                if(test==null){
                    //do something else
                }
            }catch(Exception e){
    
            }
    
        }
    
        private static String method(String str) throws Exception{
                if(str.equals("1")) return null;
                else if(str.equals("2")) throw new Exception("My Exception");
                else return str;          
        }
    
    }
    

    Now you can change method to something like this, it all depends on your requirement:

        private static String method(String str) throws Exception{
            try{
                if(str.equals("1")) return null;
                else if(str.equals("2")) throw new Exception("My Exception");
                else return str;
            }catch(Exception e){
                return null;
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fairly new to Java, so I hope the wording makes sense. I have some
I am fairly new to Java, and recently I was reading some material about
I have been working on Eclipse recently. I am fairly new to java programming,
I'm fairly new to Rational Functional Tester (Java) but I have one large blank.
I'm fairly new to programming in general but have been working with Java for
I'm fairly new to Java and I've been having some difficulties with Swing. I'm
I'm fairly new to Java development, and people have been suggesting I use Eclipse
I'm fairly new to Java (and stackoverflow for that matter) so I'll try my
I am a fairly new java programmer. I only have about five weeks of
I am fairly new to java and I have been struggling with this exercise

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.