Day 16: Exceptions - String to Integer

  • + 0 comments

    For JS this worked:

    function main() {
      const S = readLine();
      try {
        const num = JSON.parse(S);
        console.log(num);
      } catch (e) {
        console.log("Bad String");
      }
    }
    
    I tried this: 
    
    S = input()
    
    try:
        num=int(S)
        print(num)
    except ValueError: 
        print("Bad String")
    
    and this:
    
    S = input()
    
    try:
        num=int(S)
        print(num)
    except: 
        raise Exception("Bad String")
    

    ` None of these worked.