Java - Catch exception during string parsing

Description

Catch exception during string parsing

Demo

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        System.out.println("Input string to be parsed");
        Scanner in = new Scanner(System.in);
        try{/*from w w  w.j  a  v  a  2  s. c  om*/
            String S = in.next();
            System.out.println(""+Integer.parseInt(S));
        } catch(Exception e){
            System.out.println("Bad String");
        }
    }
}

Related Topic