Java - Write code to Try to Parse a string to int

Requirements

Write code to Try to Parse a string to int

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String value = "book2s.com";
        int defaultValue = 42;
        System.out.println(TryParse(value, defaultValue));
    }/*from   ww  w  .j ava 2  s . c  o  m*/

    public static int TryParse(String value, int defaultValue) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            return defaultValue;
        }
    }
}

Related Exercise