Try & Catch Block is a command statement? Syntax for using try & catch can use to handling hardware errors in WPF program?

Si4820 1 Reputation point
2022-02-15T10:57:18.497+00:00

Try Catch Block:

try{
statement(s)
}
catch (exceptiontype name){
statement(s)
}

Try & Catch Block is a command statement?
Syntax for using try & catch can use to handling hardware errors in WPF program? Give one example.

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2022-02-16T02:43:02.33+00:00

    Try-Catch is a statement keyword.
    ![174741-image.png

    You could refer to Structured Exception Handling for more information.

    There are two kinds of exceptions: hardware exceptions and software exceptions. Hardware exceptions are initiated by the CPU. They can result from the execution of certain instruction sequences, such as division by zero or an attempt to access an invalid memory address. Software exceptions are initiated explicitly by applications or the operating system. For example, the system can detect when an invalid parameter value is specified.

    An example of TryCatch is as follows:
    xaml:

    <StackPanel Orientation="Horizontal">  
            <TextBox Name="tb1" Width="100" Height="40" Background="LightGreen"/>  
            <TextBox Name="tb2" Width="100" Height="40" Background="LightSkyBlue"/>  
            <Button Name="btn" Click="btn_Click" Width="100" Height="40" Content="devide"/>  
        </StackPanel>  
    

    Button click event:

     private void btn_Click(object sender, RoutedEventArgs e)  
        {  
          try  
          {  
            int num1 = Int32.Parse(tb1.Text);  
            int num2 = Int32.Parse(tb2.Text);  
            var num3 = (double)(num1 / num2);  
          }  
          catch (DivideByZeroException dbze)  
          {  
            MessageBox.Show(dbze.ToString());  
          }  
        }  
    

    The result:
    174589-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.