"예외 처리 구문"의 두 판 사이의 차이

2번째 줄: 2번째 줄:
;예외 처리 구문
;예외 처리 구문


=== Bash ===
== Bash ==
[[category: Bash]]
[[category: Bash]]
<source lang='Bash'>
<source lang='Bash'>
50번째 줄: 50번째 줄:
</source>
</source>


=== C# ===
== C# ==
[[category:csharp]]
[[category:csharp]]
<source lang=CSharp>
<source lang=CSharp>

2014년 5월 18일 (일) 06:16 판

exception handling syntax
예외 처리 구문

1 Bash

#!/usr/bin/env bash
#set -e provides another error mechanism
print_error(){
	echo "there was an error"
}
trap print_error exit #list signals to trap
tempfile=`mktemp`
trap "rm $tempfile" exit
./other.sh || echo warning: other failed
echo oops)
echo never printed

2 C

#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>

enum { SOME_EXCEPTION = 1 } exception;
jmp_buf state;

int main(void)
{
  if(!setjmp(state))                      // try
  {
    if(/* something happened */)
    {
      exception = SOME_EXCEPTION;
      longjmp(state, 0);                  // throw SOME_EXCEPTION
    }
  } 
  else switch(exception)
  {             
    case SOME_EXCEPTION:                  // catch SOME_EXCEPTION
      puts("SOME_EXCEPTION caught");
      break;
    default:                              // catch ...
      puts("Some strange exception");
  }
  return EXIT_SUCCESS;
}

3 C#

public static void Main()
{
   try
   {
      // Code that could throw an exception
   }
   catch(System.Net.WebException ex)
   {
      // Process a WebException
   }
   catch(System.Exception)
   {
      // Process a System level CLR exception, that is not a System.Net.WebException,
      // since the exception has not been given an identifier it cannot be referenced
   }
   catch
   {
      // Process a non-CLR exception
   }
   finally
   {
      // (optional) code that will *always* execute
   }
}

4 같이 보기

5 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}