Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

Who work faster if or try catch in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        long t1 = System.currentTimeMillis();

        for (int i = 0; i < 1000000; i++) {

            int n = 0;
            for (int j = 0; j < 100; j++) {
                if (j != 0) {
                    n += 10 / j;
                    //System.out.println(n);
                }
            }
        }

        long t2 = System.currentTimeMillis();

        for (int i = 0; i < 1000000; i++) {

            int n = 0;
            for (int j = 0; j < 100; j++) {
                try {
                    n += 10 / j;
                    // System.out.println(n);
                } catch (Exception e) {
                    //System.out.println(e);
                }
            }
        }

        long t3 = System.currentTimeMillis();

        System.out.println("if: " + (t2 - t1));
        System.out.println("try - catch:" + (t3 - t2));
    }
}

/*
                   
run:
      
if: 310
try - catch:340
          
 */

 



answered Dec 17, 2016 by avibootz

Related questions

1 answer 146 views
1 answer 182 views
182 views asked Dec 25, 2016 by avibootz
1 answer 179 views
1 answer 149 views
149 views asked Jul 11, 2022 by avibootz
1 answer 156 views
156 views asked Apr 3, 2021 by avibootz
2 answers 269 views
1 answer 155 views
155 views asked Oct 5, 2019 by avibootz
...