Click here to Skip to main content
14,331,160 members
Rate this:
Please Sign up or sign in to vote.
See more:
this my code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionHandling
{
    class Program
    {
        static void Main(string[] args)
        {
            double bil1, bil2, hasil = 0;
            try
            {
                Console.Write("Masukan Bilangan 1:");
                bil1 = Convert.ToDouble(Console.ReadLine());
                Console.Write("Masukan Bilangan 2:");
                bil2 = Convert.ToDouble(Console.ReadLine());
                hasil = bil1 / bil2;
                Console.WriteLine("Hasilnya :" + hasil.ToString());
            }
            catch (FormatException fex)
            {
                Console.WriteLine("Error : Format Input tidak sesuai..");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error : " + ex.Message);
            }
            byte nilai = 0;
            try
            {
                Console.Write("Masukan Nilai :");
                nilai = Convert.ToByte(Console.ReadLine());
            }
            catch (OverflowException ofex)
            {
                Console.WriteLine("Error : Tipe data tidak cukup menampung nilai");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error :" + ex.Message);

                Lingkaran objLing = new Lingkaran();
                try
                {
                    Console.Write("Masukan diameter :");
                    objLing.Diameter = Convert.ToDouble(Console.ReadLine());
                }
            }
        }
    }
}

What I have tried:

help me.. How to solve expected catch or finaly my code
Posted
Updated 25-Apr-19 3:42am
Comments
Christian Graus 25-Apr-19 0:13am
   
It's expecting a generic catch or a finally at the end, for when the type of exception is unknown. The error means what it says :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

Instead of using Try/Catch, use the conversion operators that do not throw an error if the input is incorrect:
using System;

namespace YourConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int bill1;

            int tryLimit = 3;

            int tryCnt = 0;

            Console.WriteLine("enter an integer");

            while (tryCnt < tryLimit && !Int32.TryParse(Console.ReadLine(), out bill1))
            {
                tryCnt++;

                // invalid input
                if (tryCnt < tryLimit) Console.WriteLine("invalid input ... enter an integer");

            }

            if (tryCnt == tryLimit)
            {
                Console.WriteLine("too many incorrect entry attempts");
                Console.Write("?");
                Console.ReadLine();
                // exit the app here ?
            }
            else
            {
                // you now have a valid integer in the variable 'bill1
            }
        }
    }
}
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

You have a try block at the end which has neither a catch block nor a finally block:
Quote:
catch (Exception ex)
{
    Console.WriteLine("Error :" + ex.Message);
    
    Lingkaran objLing = new Lingkaran();
    try
    {
        Console.Write("Masukan diameter :");
        objLing.Diameter = Convert.ToDouble(Console.ReadLine());
    }
    
    // No "catch" or "finally" block here...
    
}

To fix the syntax error, either add a catch or a finally block, or remove the try block.
catch (Exception ex)
{
    Console.WriteLine("Error :" + ex.Message);
    
    Lingkaran objLing = new Lingkaran();
    Console.Write("Masukan diameter :");
    objLing.Diameter = Convert.ToDouble(Console.ReadLine());
}


But as Bill said, it's better to use Double.TryParse to try to convert a string to a number, rather than using Convert.ToDouble and catching the exception.
Double.TryParse Method (System) | Microsoft Docs[^]
   
Comments
BillWoodruff 27-Apr-19 2:08am
   
+5
Rate this:
Please Sign up or sign in to vote.

Solution 1

It's expecting a generic catch or a finally at the end, for when the type of exception is unknown. The error means what it says :)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100