14 Replies - 323 Views - Last Post: 14 June 2019 - 02:37 PM

#1 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 06:24 AM

Can someone double check my code?

I passed an invalid address for Bitcoin on purpose to see if my catch would work but instead it crashed and i viewed the errors in platform tools/adb.exe logs.

if(type.equals("BTC")) {
    try {
        new Address(MainPage.BTCparams, receiver);
        try {
            String value1 = String.format("%.8f", Double.parseDouble(amount));
            Coin val1 = Coin.parseCoin(value1);
            Address target1 = Address.fromBase58(MainPage.BTCparams, receiver);
            Transaction tx = new Transaction(MainPage.BTCparams);
            tx.addOutput(val1, target1);
            SendRequest req = SendRequest.forTx(tx);
            req.feePerKb = Coin.parseCoin("0.0001");
            MainPage.BTCkit.wallet().completeTx(req);
            MainPage.BTCkit.wallet().commitTx(req.tx);
            MainPage.BTCkit.peerGroup().broadcastTransaction(req.tx);
            String[] temp = req.tx.getFee().toFriendlyString().split(" ");
            send[0] = amount;
            send[1] = temp[0];
            send[2] = req.tx.getHashAsString();
            Toast.makeText(context,"Refund Sent!",Toast.LENGTH_SHORT).show();
            return send;
        } catch (InsufficientMoneyException e) {
            Double mis = (double) e.missing.getValue() / 100000000;
            send[0] = String.valueOf(mis);
            send[1] = "0.00000000";
            send[2] = null;
            Toast.makeText(context,"Sending failed!",Toast.LENGTH_SHORT).show();
            return send;
        }
    }catch(WrongNetworkException e){
        Toast.makeText(context, "This address is not valid for this network.", Toast.LENGTH_SHORT).show();
    }catch(AddressFormatException e) {
        Toast.makeText(context, "This address is not valid.", Toast.LENGTH_SHORT).show();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: difficulty attempting to catch invalid address string.

#2 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 706
  • Joined: 23-September 15

Re: difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 06:27 AM

What is the exception?
Was This Post Helpful? 0
  • +
  • -

#3 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 07:01 AM

addressformatexception.

When the app crashes and i check the logs thats the error it comes up. I was expecting this error to be thrown but how can i design it so it doesnt crsah and simply displays the toast?
Was This Post Helpful? 0
  • +
  • -

#4 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 706
  • Joined: 23-September 15

Re: difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 08:49 AM

Try use catch(Exception e){} instead
Was This Post Helpful? 0
  • +
  • -

#5 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 08:49 AM

the documentation for this library is located here.

https://bitcoinj.git...re/Address.html

in response to your new reply, what would that do? its going to run nothing?

This post has been edited by Bobby_Bubbles: 12 June 2019 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 706
  • Joined: 23-September 15

Re: difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 08:55 AM

I guess the reason it doesn't go into the exception is because you are catching the wrong exception?
Give this as try and let me know
catch(Exception e){
 Toast.makeText(context,"Sending failed!",Toast.LENGTH_SHORT).show();
} 

This post has been edited by wseng92: 12 June 2019 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#7 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 09:10 AM

public Address(@Nullable
               NetworkParameters params,
               String address)
        throws AddressFormatException
Construct an address from parameters and the standard "human readable" form. Example:
new Address(MainNetParams.get(), "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
Parameters:
params - The expected NetworkParameters or null if you don't want validation.
address - The textual form of the address, such as "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL"
Throws:
AddressFormatException - if the given address doesn't parse or the checksum is invalid
WrongNetworkException - if the given address is valid but for a different chain (eg testnet vs mainnet)



This is the documentation. i caught both exceptions in those claus but it still crashes and just restarts. Hold on i may have found the issue.

Does exception act as a catch all exception?
Was This Post Helpful? 0
  • +
  • -

#8 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 09:19 AM

i found the issue but it wasnt related to this.

i kept it simple and just did this after it reads the QRCode

try{
            new Address(MainPage.BTCparams, link[2]);
            Rconfirm();
        }catch(WrongNetworkException e){
            Toast.makeText(context, "This address is not valid for this network.", Toast.LENGTH_SHORT).show();
            setContentView(R.layout.activity_index);
        }catch(AddressFormatException e) {
            Toast.makeText(context, "This address is not valid.", Toast.LENGTH_SHORT).show();
            setContentView(R.layout.activity_index);
        }catch(Exception e){
            Toast.makeText(context, "This address is not valid for unknown reason.", Toast.LENGTH_SHORT).show();
            setContentView(R.layout.activity_index);
        }


Was This Post Helpful? 0
  • +
  • -

#9 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 706
  • Joined: 23-September 15

Re: difficulty attempting to catch invalid address string.

Posted 12 June 2019 - 09:31 AM

And it still doesn't run through the catch ?
Was This Post Helpful? 0
  • +
  • -

#10 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: difficulty attempting to catch invalid address string.

Posted 14 June 2019 - 06:12 AM

yea it catches now. but the exception one doesnt catch at all.


issue resolved.
Was This Post Helpful? 0
  • +
  • -

#11 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: difficulty attempting to catch invalid address string.

Posted 14 June 2019 - 07:31 AM

i do have another issue relating to this ina minor way.

MainPage.BTCkit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
                @Override
                public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
                    TextView tVStatus = (TextView)findViewById(R.id.tVStatus);
                    tVStatus.setText("Testing");
                }
            });



When this gets triggered it rarely sets text to testing and if it does its minutes gone by or leave the app then resume.

Im designing this app for a single purpose device.

More inf check here...https://developer.android.com/work/dpc/dedicated-devices
Was This Post Helpful? 0
  • +
  • -

#12 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 706
  • Joined: 23-September 15

Re: difficulty attempting to catch invalid address string.

Posted 14 June 2019 - 07:38 AM

Quote

When this gets triggered it rarely sets text to testing and if it does its minutes gone by or leave the app then resume.

Mean it crashed.

This post has been edited by wseng92: 14 June 2019 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#13 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: difficulty attempting to catch invalid address string.

Posted 14 June 2019 - 08:03 AM

no the crashing was resolved the the post before.
Was This Post Helpful? 0
  • +
  • -

#14 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 55
  • View blog
  • Posts: 706
  • Joined: 23-September 15

Re: difficulty attempting to catch invalid address string.

Posted 14 June 2019 - 08:08 AM

So you hitting another issue?

Quote

if it does its minutes gone by or leave the app then resume

This sounds like crashed.

This post has been edited by wseng92: 14 June 2019 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#15 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: difficulty attempting to catch invalid address string.

Posted 14 June 2019 - 02:37 PM

but there is no logs or any dialog.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1