## Algorithm

Problem Name: 30 days of code - Day 16: Exceptions - String to Integer

Objective
Today, we're getting started with Exceptions by learning how to parse an integer from a string and print a custom error message. Check out the Tutorial tab for learning materials and an instructional video!

Read a string,  S, and print its integer value; if S cannot be converted to an integer, print `Bad String`. Note: You must use the String-to-Integer and exception handling constructs built into your submission language. If you attempt to use loops/conditional statements, you will get a 0 score.

Input Format

A single string S.

Constraints

• 1 <= |S| <= 6, where |S| is the length of string S.
• S is composed of either lowercase letters (a - z) or decimal digits (0 - 9).

Output Format

Print the parsed integer value of S, or `Bad String` if S cannot be converted to an integer.

Sample Input 0

``````3
``````

Sample Output 0

``````3
``````

Sample Input 1

``````za
``````

Sample Output 1

``````Bad String
``````

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````
#include <iostream>

using namespace std;

int main() {
try {
string str;
cin >> str;

int num = stoi(str);
cout << num;
}
catch (...) {
}
return 0;
}

``````
Copy The Code &

### #2 Code Example with C# Programming

```Code - C# Programming```

``````
using System;

class Solution
{
static void Main(String[] args)
{

try
{
int num = int.Parse(str);
Console.WriteLine(num);
}
catch (Exception)
{
}
}
}
``````
Copy The Code &

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String string = in.next();
in.close();
try {
Integer integer = Integer.parseInt(string);
System.out.println(integer);
} catch (NumberFormatException numberFormatException) {
}
}
}
``````
Copy The Code &

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});

process.stdin.on('end', function() {
inputString = inputString.split('\n');

main();
});

return inputString[currentLine++];
}

function main() {

try {
isNaN(Number(S)) ? error : console.log(S)
}
catch (err) {
}
}
``````
Copy The Code &

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
try:
print(int(input().strip()))
except ValueError:
``````
Copy The Code &

### #6 Code Example with PHP Programming

```Code - PHP Programming```

``````

``````
Copy The Code &